Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
 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)
     }));
 }
Exemplo n.º 4
0
        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));
        }