public async Task<bool> LoginUserAsync (User user) { // var authData = string.Format ("{0}:{1}", user.Username, user.Password); // var authHeaderValue = Convert.ToBase64String (Encoding.UTF8.GetBytes (authData)); // // client = new HttpClient (); // client.MaxResponseContentBufferSize = 256000; // client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue ("Basic", authHeaderValue); var handler = new HttpClientHandler { Credentials = new NetworkCredential(user.Username, user.Password) }; // Use username/password credentials. client = new HttpClient(handler); client.MaxResponseContentBufferSize = 256000; var uri = new Uri (string.Format (Constants.RestUrl, string.Empty)); bool auth = false; Debug.WriteLine("GET: " + uri); try { HttpResponseMessage response = await client.GetAsync (uri); HttpContent content = response.Content; // Check status code. Debug.WriteLine("Response StatusCode: " + (int)response.StatusCode); // Read the string. string result = await content.ReadAsStringAsync(); // Display the result. if (result != null && result.Length >= 50) { // Debug.WriteLine(result.Substring(0, 50) + "..."); Debug.WriteLine(result); } if (response.IsSuccessStatusCode) { auth = true; return auth; } } catch (Exception ex) { Debug.WriteLine (@"ERROR {0}", ex.Message); } return auth; }
public Task<bool> CheckCredentialAsync(User user) { return service.LoginUserAsync (user); }