コード例 #1
0
        /// <summary>
        /// Sends a token request.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, TokenRequest request, CancellationToken cancellationToken = default)
        {
            if (!request.Parameters.ContainsKey(OidcConstants.TokenRequest.GrantType))
            {
                request.Parameters.AddRequired(OidcConstants.TokenRequest.GrantType, request.GrantType);
            }

            var httpRequest = new HttpRequestMessage(HttpMethod.Post, request.Address);

            httpRequest.Headers.Accept.Clear();
            httpRequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            ClientCredentialsHelper.PopulateClientCredentials(request, httpRequest);
            httpRequest.Content = new FormUrlEncodedContent(request.Parameters);

            HttpResponseMessage response;

            try
            {
                response = await client.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                return(new TokenResponse(ex));
            }

            string content = null;

            if (response.Content != null)
            {
                content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
            }

            if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.BadRequest)
            {
                return(new TokenResponse(content));
            }
            else
            {
                return(new TokenResponse(response.StatusCode, response.ReasonPhrase, content));
            }
        }
コード例 #2
0
        /// <summary>
        /// Sends a token request.
        /// </summary>
        /// <param name="client">The client.</param>
        /// <param name="request">The request.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns></returns>
        public static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, TokenRequest request, CancellationToken cancellationToken = default)
        {
            var clone = request.Clone();

            if (!clone.Parameters.ContainsKey(OidcConstants.TokenRequest.GrantType))
            {
                clone.Parameters.AddRequired(OidcConstants.TokenRequest.GrantType, request.GrantType);
            }

            return(await client.RequestTokenAsync(clone, cancellationToken).ConfigureAwait(false));
        }
コード例 #3
0
        public static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, TokenRequest request, CancellationToken?cancellationToken = null)
        {
            Request request1 = request.Clone();

            if (!request1.Parameters.ContainsKey("grant_type"))
            {
                request1.Parameters.AddRequired("grant_type", request.GrantType, false);
            }
            ConfiguredTaskAwaitable <TokenResponse> configuredTaskAwaitable = HttpClientTokenRequestExtensions.RequestTokenAsync(client, request1, cancellationToken).ConfigureAwait(false);

            return(await configuredTaskAwaitable);
        }