Exemple #1
0
        /// <summary>
        /// Clones this instance.
        /// </summary>
        /// <returns></returns>
        public ProtocolRequest Clone()
        {
            var clone = new ProtocolRequest
            {
                Address = Address,
                AuthorizationHeaderStyle = AuthorizationHeaderStyle,
                ClientAssertion          = ClientAssertion,
                ClientCredentialStyle    = ClientCredentialStyle,
                ClientId     = ClientId,
                ClientSecret = ClientSecret,
                Parameters   = new Dictionary <string, string>()
            };

            if (Parameters != null)
            {
                foreach (var item in Parameters)
                {
                    clone.Parameters.Add(item);
                }
            }

            return(clone);
        }
        internal static async Task <IdentityModel.Client.IdentityModelExtensions.TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken?cancellationToken)
        {
            request.Prepare();
            request.Method = HttpMethod.Post;

            HttpResponseMessage response;

            try
            {
                response = await client.SendAsync(request, cancellationToken.HasValue?cancellationToken.Value : new CancellationToken()).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                return(ProtocolResponse.FromException <IdentityModel.Client.IdentityModelExtensions.TokenResponse>(ex));
            }

            return(await ProtocolResponse.FromHttpResponseAsync <IdentityModel.Client.IdentityModelExtensions.TokenResponse>(response).ConfigureAwait(false));
        }
        internal static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken cancellationToken = default)
        {
            request.Prepare();
            request.Method = HttpMethod.Post;

            HttpResponseMessage response;

            try
            {
                response = await client.SendAsync(request, cancellationToken).ConfigureAwait();
            }
            catch (Exception ex)
            {
                return(ProtocolResponse.FromException <TokenResponse>(ex));
            }

            return(await ProtocolResponse.FromHttpResponseAsync <TokenResponse>(response).ConfigureAwait());
        }
        internal static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken cancellationToken = default)
        {
            if (!request.Parameters.TryGetValue(OidcConstants.TokenRequest.ClientId, out _))
            {
                if (request.ClientId.IsMissing())
                {
                    throw new InvalidOperationException("client_id is missing");
                }
            }

            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(ProtocolResponse.FromException <TokenResponse>(ex));
            }

            return(await ProtocolResponse.FromHttpResponseAsync <TokenResponse>(response).ConfigureAwait(false));
        }
Exemple #5
0
        internal static async Task <TokenResponse> RequestTokenAsync(this HttpMessageInvoker client, ProtocolRequest request, CancellationToken cancellationToken = default)
        {
            if (!request.Parameters.TryGetValue(OidcConstants.TokenRequest.ClientId, out _))
            {
                if (request.ClientId.IsMissing())
                {
                    throw new InvalidOperationException("client_id is missing");
                }
            }

            request.Prepare();
            request.Method = HttpMethod.Post;

            HttpResponseMessage response;

            try
            {
                response = await client.SendAsync(request, cancellationToken).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                return(ProtocolResponse.FromException <TokenResponse>(ex));
            }

            return(await ProtocolResponse.FromHttpResponseAsync <TokenResponse>(response).ConfigureAwait(false));
        }