public void GetUserInfo(string accessToken)
        {
            // in order to get access to additional token "profile" scope must be requested
            var infoRequest = new IdentityModel.Client.UserInfoRequest
            {
                Token   = accessToken,
                Address = $"{authserverurl}/connect/userinfo"
            };

            var client           = new HttpClient();
            var userinforesponse = client.GetUserInfoAsync(infoRequest);

            if (userinforesponse.Result.IsError)
            {
                Console.WriteLine(userinforesponse.Result.Error);
                return;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Sends a userinfo 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 <UserInfoResponse> GetUserInfoAsync(this HttpMessageInvoker client, UserInfoRequest request, CancellationToken cancellationToken = default)
        {
            if (request.Token.IsMissing())
            {
                throw new ArgumentNullException(nameof(request.Token));
            }

            var clone = request.Clone();

            clone.Method = HttpMethod.Get;
            clone.SetBearerToken(request.Token);
            clone.Prepare();

            HttpResponseMessage response;

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

            return(await ProtocolResponse.FromHttpResponseAsync <UserInfoResponse>(response).ConfigureAwait(false));
        }
        /// <summary>
        /// Sends a userinfo 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 <UserInfoResponse> GetUserInfoAsync(this HttpClient client, UserInfoRequest request, CancellationToken cancellationToken = default)
        {
            if (request.Token.IsMissing())
            {
                throw new ArgumentNullException(nameof(request.Token));
            }

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

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

            HttpResponseMessage response;

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

            if (!response.IsSuccessStatusCode)
            {
                return(new UserInfoResponse(response.StatusCode, response.ReasonPhrase));
            }

            var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

            return(new UserInfoResponse(content));
        }
        /// <summary>
        /// Sends a userinfo 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 <UserInfoResponse> GetUserInfoAsync(this HttpMessageInvoker client, UserInfoRequest request, CancellationToken cancellationToken = default)
        {
            if (request.Token.IsMissing())
            {
                throw new ArgumentNullException(nameof(request.Token));
            }

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

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

            HttpResponseMessage response;

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

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