static public bool login(string username, string password) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); // Create a from object var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", username), new KeyValuePair <string, string>("password", password), }); // Post the form to the Toekn end point var result = client.PostAsync(baseWebAddress + "Token", content).Result; try { // Make sure System.Net.Http.Extensoins is referenced through Nuget // For async http action calls var resultContent = result.Content.ReadAsAsync <Token>( new[] { new JsonMediaTypeFormatter() } ).Result; string ServerError = string.Empty; if (!(String.IsNullOrEmpty(resultContent.AccessToken))) { Console.WriteLine(resultContent.AccessToken); Token = resultContent.AccessToken; Status = AUTHSTATUS.OK; return(true); } else { Token = "Invalid Login"; Status = AUTHSTATUS.INVALID; Console.WriteLine("Invalid credentials"); return(false); } } catch (Exception ex) { Status = AUTHSTATUS.FAILED; Token = "Server Error -> " + ex.Message; Console.WriteLine(ex.Message); return(false); } } }
static async public Task<bool> login(string username, string password) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var content = new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("grant_type", "password"), new KeyValuePair<string, string>("username", username), new KeyValuePair<string, string>("password", password), }); var result = client.PostAsync(baseWebAddress + "Token", content).Result; try { var resultContent = result.Content.ReadAsAsync<Token>( new[] { new JsonMediaTypeFormatter() } ).Result; string ServerError = string.Empty; if (!(String.IsNullOrEmpty(resultContent.AccessToken))) { Console.WriteLine(resultContent.AccessToken); PlayerToken = resultContent.AccessToken; PlayerStatus = AUTHSTATUS.OK; return true; } else { PlayerToken = "Invalid Login" ; PlayerStatus = AUTHSTATUS.INVALID; Console.WriteLine("Invalid credentials"); return false; } } catch (Exception ex) { PlayerStatus = AUTHSTATUS.FAILED; PlayerToken = "Server Error -> " + ex.Message; Console.WriteLine(ex.Message); return false; } PlayerStatus = AUTHSTATUS.INVALID; return false; } }
static async public Task <bool> login(string username, string password) { using (var client = new HttpClient()) { client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var content = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("grant_type", "password"), new KeyValuePair <string, string>("username", username), new KeyValuePair <string, string>("password", password), }); var result = client.PostAsync(baseWebAddress + "Token", content).Result; try { var resultContent = result.Content.ReadAsAsync <Token>( new[] { new JsonMediaTypeFormatter() } ).Result; string ServerError = string.Empty; if (!(String.IsNullOrEmpty(resultContent.AccessToken))) { Console.WriteLine(resultContent.AccessToken); PlayerToken = resultContent.AccessToken; PlayerStatus = AUTHSTATUS.OK; return(true); } else { PlayerToken = "Invalid Login"; PlayerStatus = AUTHSTATUS.INVALID; Console.WriteLine("Invalid credentials"); return(false); } } catch (Exception ex) { PlayerStatus = AUTHSTATUS.FAILED; PlayerToken = "Server Error -> " + ex.Message; Console.WriteLine(ex.Message); return(false); } PlayerStatus = AUTHSTATUS.INVALID; return(false); } }