private async Task <string> GetAuthToken() { if (!HasValidAuthToken) { var service = new HpeFortifyService(_clientId, _clientSecret); _authResponse = await service.Login(); _authExpiration = DateTime.Now.AddSeconds(Int32.Parse(_authResponse.expires_in)); } return(_authResponse.access_token); }
private async Task VerifyLoggedIn() { if (String.IsNullOrEmpty(AuthResponse?.access_token)) { AuthResponse = await Login(); if (String.IsNullOrEmpty(AuthResponse.access_token)) { throw new HttpRequestException("Authentication failure"); } //AuthToken = loginResponse.access_token; RestClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AuthResponse.access_token); } }
public async Task <HpeAuthResponse> Login() { ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; HpeAuthResponse authResponse = null; var authClient = new HttpClient { BaseAddress = new Uri(HpeUrlRoot) }; authClient.DefaultRequestHeaders.Accept.Clear(); authClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var formContent = new FormUrlEncodedContent(new[] { new KeyValuePair <string, string>("scope", AuthScope), new KeyValuePair <string, string>("grant_type", AuthGrantType), new KeyValuePair <string, string>("client_id", ClientId), new KeyValuePair <string, string>("client_secret", ClientSecret) }); try { HttpResponseMessage response = await authClient.PostAsync(AuthEndpoint, formContent); if (response.StatusCode == System.Net.HttpStatusCode.OK) { AuthResponse = await response.Content.ReadAsAsync <HpeAuthResponse>(); //AuthToken = authResponse.access_token; // Token is good for 6 hours (21600 / 60 = 360; 360 / 60 = 6) } return(AuthResponse); } catch (Exception ex) { Console.WriteLine(ex.ToString()); throw; } }