Пример #1
0
        private static void Introspection(string accessToken)
        {
            var client = new IntrospectionClient(
                "https://localhost:44333/core/connect/introspect",
                "write",
                "secret");

            var request = new IntrospectionRequest
            {
                Token = accessToken
            };

            var result = client.SendAsync(request).Result;

            if (result.IsError)
            {
                Console.WriteLine(result.Error);
            }
            else
            {
                if (result.IsActive)
                {
                    result.Claims.ToList().ForEach(c => Console.WriteLine("{0}: {1}",
                        c.Item1, c.Item2));
                }
                else
                {
                    Console.WriteLine("token is not active");
                }
            }
        }
Пример #2
0
        private static void Introspection(string accessToken)
        {
            var client = new IntrospectionClient(
                Constants.IntrospectionEndpoint,
                "api1",
                "secret");

            var request = new IntrospectionRequest
            {
                Token = accessToken
            };

            var result = client.SendAsync(request).Result;

            if (result.IsError)
            {
                Console.WriteLine(result.Error);
            }
            else
            {
                if (result.IsActive)
                {
                    result.Claims.ToList().ForEach(c => Console.WriteLine("{0}: {1}",
                        c.Item1, c.Item2));
                }
                else
                {
                    Console.WriteLine("token is not active");
                }
            }
        }
Пример #3
0
        public async Task <IntrospectionResponse> SendAsync(IntrospectionRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }
            if (string.IsNullOrWhiteSpace(request.Token))
            {
                throw new ArgumentNullException("token");
            }

            var form = new Dictionary <string, string>();

            form.Add("token", request.Token);

            if (!string.IsNullOrWhiteSpace(request.TokenTypeHint))
            {
                form.Add("token_type_hint", request.TokenTypeHint);
            }

            if (!string.IsNullOrWhiteSpace(request.ClientId))
            {
                form.Add("client_id", request.ClientId);
            }
            else if (!string.IsNullOrWhiteSpace(_clientId))
            {
                form.Add("client_id", _clientId);
            }

            if (!string.IsNullOrWhiteSpace(request.ClientSecret))
            {
                form.Add("client_secret", request.ClientSecret);
            }

            try
            {
                var response = await _client.PostAsync("", new FormUrlEncodedContent(form)).ConfigureAwait(false);

                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return(new IntrospectionResponse
                    {
                        IsError = true,
                        Error = response.ReasonPhrase
                    });
                }

                return(new IntrospectionResponse(await response.Content.ReadAsStringAsync().ConfigureAwait(false)));
            }
            catch (Exception ex)
            {
                return(new IntrospectionResponse
                {
                    IsError = true,
                    Error = ex.Message
                });
            }
        }
Пример #4
0
        /// <summary>
        /// Sends the introspection request.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">
        /// request
        /// or
        /// Token
        /// </exception>
        public virtual async Task <IntrospectionResponse> SendAsync(IntrospectionRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (request.Token.IsMissing())
            {
                throw new ArgumentNullException(nameof(request.Token));
            }

            IDictionary <string, string> form;

            if (request.Parameters == null)
            {
                form = new Dictionary <string, string>();
            }
            else
            {
                form = request.Parameters;
            }

            form.Add("token", request.Token);

            if (request.ClientId.IsPresent())
            {
                form.Add("client_id", request.ClientId);
            }
            else if (ClientId.IsPresent())
            {
                form.Add("client_id", ClientId);
            }

            form.AddIfPresent("token_type_hint", request.TokenTypeHint);
            form.AddIfPresent("client_secret", request.ClientSecret);

            HttpResponseMessage response;

            try
            {
                response = await Client.PostAsync("", new FormUrlEncodedContent(form)).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                return(new IntrospectionResponse(ex));
            }
            if (response.IsSuccessStatusCode)
            {
                return(new IntrospectionResponse(await response.Content.ReadAsStringAsync().ConfigureAwait(false)));
            }
            else
            {
                return(new IntrospectionResponse(response.StatusCode, response.ReasonPhrase));
            }
        }
Пример #5
0
        public async Task<IntrospectionResponse> SendAsync(IntrospectionRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (string.IsNullOrWhiteSpace(request.Token)) throw new ArgumentNullException("token");

            var form = new Dictionary<string, string>();
            form.Add("token", request.Token);

            if (!string.IsNullOrWhiteSpace(request.TokenTypeHint))
            {
                form.Add("token_type_hint", request.TokenTypeHint);
            }

            if (!string.IsNullOrWhiteSpace(request.ClientId))
            {
                form.Add("client_id", request.ClientId);
            }
            else if (!string.IsNullOrWhiteSpace(_clientId))
            {
                form.Add("client_id", _clientId);
            }

            if (!string.IsNullOrWhiteSpace(request.ClientSecret))
            {
                form.Add("client_secret", request.ClientSecret);
            }

            try
            {
                var response = await _client.PostAsync("", new FormUrlEncodedContent(form)).ConfigureAwait(false);
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return new IntrospectionResponse
                    {
                        IsError = true,
                        Error = response.ReasonPhrase
                    };
                }

                return new IntrospectionResponse(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
            }
            catch (Exception ex)
            {
                return new IntrospectionResponse
                {
                    IsError = true,
                    Error = ex.Message
                };
            }
        }
Пример #6
0
        /// <summary>Sends the introspection request.</summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">request or Token</exception>
        public async Task <IntrospectionResponse> SendAsync(IntrospectionRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }
            if (string.IsNullOrWhiteSpace(request.Token))
            {
                throw new ArgumentNullException(nameof(request.Token));
            }

            var form = new Dictionary <string, string>(StringComparer.Ordinal)
            {
                { "token", request.Token }
            };

            if (!string.IsNullOrWhiteSpace(request.TokenTypeHint))
            {
                form.Add("token_type_hint", request.TokenTypeHint);
            }

            if (!string.IsNullOrWhiteSpace(request.ClientId))
            {
                form.Add("client_id", request.ClientId);
            }
            else if (!string.IsNullOrWhiteSpace(_clientId))
            {
                form.Add("client_id", _clientId);
            }

            if (!string.IsNullOrWhiteSpace(request.ClientSecret))
            {
                form.Add("client_secret", request.ClientSecret);
            }

            //try
            //{
            //  var response = await _client.PostAsync("", new FormUrlEncodedContent(form)).ConfigureAwait(false);
            //  if (response.StatusCode != HttpStatusCode.OK)
            //  {
            //    return new IntrospectionResponse
            //    {
            //      IsError = true,
            //      Error = response.ReasonPhrase
            //    };
            //  }

            //  return new IntrospectionResponse(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
            //}
            //catch (Exception ex)
            //{
            //  return new IntrospectionResponse
            //  {
            //    IsError = true,
            //    Error = ex.Message
            //  };
            //}
            HttpResponseMessage response;

            try
            {
                response = await _client.PostAsync("", new FormUrlEncodedContent(form)).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                return(new IntrospectionResponse(ex));
            }
            if (response.IsSuccessStatusCode)
            {
                return(new IntrospectionResponse(await response.Content.ReadAsStringAsync().ConfigureAwait(false)));
            }
            else
            {
                return(new IntrospectionResponse(response.StatusCode, response.ReasonPhrase));
            }
        }