public async Task <bool> AuthAsync(string refreshToken) { try { Console.WriteLine(" Info: Refreshing Token"); using (var httpClient = new HttpClient()) { var authData = new { RefreshToken = refreshToken, GrantType = "refresh_token" }; var authDataJson = System.Text.Json.JsonSerializer.Serialize(authData); var authDataContent = new StringContent(authDataJson, Encoding.UTF8, "application/json"); var authMessage = await httpClient.PostAsync($"{this.apiSettings.Url}/api/users/auth", authDataContent); var authResultContent = await authMessage.Content.ReadAsStringAsync(); if (!authMessage.IsSuccessStatusCode) { Console.WriteLine($" AuthResult: {authResultContent}"); } this.Token = System.Text.Json.JsonSerializer.Deserialize <ApiToken>(authResultContent); if (this.Token == null) { Console.WriteLine(" Warning: Could not authenticate on api"); return(false); } return(true); } } catch (Exception ex) { Console.WriteLine($" Exception: {ex.Message}"); return(false); } }
public async Task <bool> AuthAsync() { try { Console.WriteLine(" Info: Authenticating on api with the provided credentials"); using (var httpClient = new HttpClient()) { var authData = new { UserName = this.apiSettings.Username, Password = this.apiSettings.Password, GrantType = "password" }; var authDataJson = System.Text.Json.JsonSerializer.Serialize(authData); var authDataContent = new StringContent(authDataJson, Encoding.UTF8, "application/json"); var authMessage = await httpClient.PostAsync($"{this.apiSettings.Url}/api/users/auth", authDataContent); var authResultContent = await authMessage.Content.ReadAsStringAsync(); if (!authMessage.IsSuccessStatusCode) { Console.WriteLine($" AuthResult: {authResultContent}"); } this.Token = System.Text.Json.JsonSerializer.Deserialize <ApiToken>(authResultContent); if (this.Token == null) { Console.WriteLine(" Warning: Could not authenticate on api"); return(false); } Console.WriteLine($" UserID: {this.Token.UserID}"); return(true); } } catch (Exception ex) { Console.WriteLine($" Exception: {ex.Message}"); return(false); } }