Xamarin form : Token authentication with xamarin form web api

i am working with token based authentication for xamarin form here is my code

 public async Task<string> checkLogin(string username, string password)

    var keyValues = new List<KeyValuePair<string, string>>
        new KeyValuePair<string, string>("username",username),
        new KeyValuePair<string, string>("password",password),
                        new KeyValuePair<string, string>("grant_type","password")

    var request = new HttpRequestMessage(HttpMethod.Post, "http://softsol.com.pk/Token");

    request.Content = new FormUrlEncodedContent(keyValues);

    var client = new HttpClient();

    var response = await client.SendAsync(request);
    var content = await response.Content.ReadAsStringAsync();

    JObject jdynamic = JsonConvert.DeserializeObject<dynamic>(content);
    var accessToken = jdynamic.Value<string>("access_token");
    var refershToken = jdynamic.Value<string>("refresh_token");
    var date = DateTime.Now.ToString();
    var accessTokenExpiration= jdynamic.Value< DateTime> (".expires");

    //await Application.Current.MainPage.DisplayAlert("Alert", content, "OK");
    Settings.RefreshToken = refershToken;

    Settings.AccessTokenexpiration = accessTokenExpiration;
    return accessToken;
    //return response.IsSuccessStatusCode;


when i login with usernamre and password i get this error

Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: <. Path '', line 0, position 0

and if i run in postman its working fine for example if i run this http://softsol.com.pk/Token in postman with username="test",password="test", grant-type="password" i get the token and its working but i get error from xamarin form. And i am stuck here

Best Answer


Sign In or Register to comment.