public async Task <string> GenerateToken(string email, string password) { var client = new HttpClient { BaseAddress = new Uri(this._iconfiguration["Identity:Authority"]) }; var parameters = new Dictionary <string, string> { { "grant_type", "password" }, { "scope", "openid api1" }, { "client_id", "rel.angular" }, { "client_secret", "secret" }, { "username", string.Empty + email + string.Empty }, { "password", string.Empty + password + string.Empty } }; var response = await client.PostAsync("/connect/token", HttpClientHelpers.GetPostBody(parameters)); response.EnsureSuccessStatusCode(); var responseBody = await response.Content.ReadAsStringAsync(); var token = JsonConvert.DeserializeObject <TokenDetail>(responseBody); return(token.access_token); }
public async Task <GetAccessTokenFromSecretKeyResponse> GetAccessTokenFromSecretKey(string secretKey, string clientId) { var client = new HttpClient() { BaseAddress = new Uri(_authUri) }; client.DefaultRequestHeaders.Authorization = BasicAuthHeader.GetHeader(clientId, secretKey); var parameters = new Dictionary <string, string>() { { "grant_type", "client_credentials" }, { "scope", "api1" } }; var clientResponse = await client.PostAsync("connect/token", HttpClientHelpers.GetPostBody(parameters)); var response = await QuickResponse <GetAccessTokenFromSecretKeyResponse> .FromMessage(clientResponse); if (response.Data?.AccessToken == null) { throw new Exception($"Could not get access token. Error: {response.ResponseBody}"); } return(response.Data); }
protected async Task <QuickResponse <T> > PostHttp <T>(string url, Dictionary <string, string> parameters) { return(await SendHttp <T>(() => new HttpRequestMessage(HttpMethod.Post, Options.BaseUri + url) { Content = HttpClientHelpers.GetPostBody(parameters) })); }
protected async Task <QuickResponse <T> > PostHttp <T>(string url, Dictionary <string, string> parameters) { await SetInitialToken(); var response = await _client.PostAsync(url, HttpClientHelpers.GetPostBody(parameters)); if (response.StatusCode == HttpStatusCode.Unauthorized) { await GetToken(); response = await _client.PostAsync(url, HttpClientHelpers.GetPostBody(parameters)); } return(await QuickResponse <T> .FromMessage(response)); }