public static TestServer CreateServer(OAuth2IntrospectionOptions options)
        {
            return new TestServer(TestServer.CreateBuilder().UseStartup(
                app =>
                {
                    app.UseOAuth2IntrospectionAuthentication(options);

                    app.Use((context, next) =>
                    {
                        var user = context.User;

                        if (user.Identity.IsAuthenticated)
                        {
                            context.Response.StatusCode = 200;
                        }
                        else
                        {
                            context.Response.StatusCode = 401;
                        }

                        return Task.FromResult(0);
                    });
                }, 
                services =>
                {
                    services.AddAuthentication();
                }));
        }
        public static IApplicationBuilder UseOAuth2IntrospectionAuthentication(this IApplicationBuilder app, OAuth2IntrospectionOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            return app.UseMiddleware<OAuth2IntrospectionMiddleware>(options);
        }
        public static IApplicationBuilder UseOAuth2IntrospectionAuthentication(this IApplicationBuilder app, Action<OAuth2IntrospectionOptions> configureOptions)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            var options = new OAuth2IntrospectionOptions();
            if (configureOptions != null)
            {
                configureOptions(options);
            }

            return app.UseMiddleware<OAuth2IntrospectionMiddleware>(options);
        }
 public static HttpMessageHandler CreateHandler(OAuth2IntrospectionOptions options)
 {
     return CreateServer(options).CreateHandler();
 }
 public static HttpClient CreateClient(OAuth2IntrospectionOptions options)
 {
     return CreateServer(options).CreateClient();
 }
        private static OAuth2IntrospectionOptions ConfigureIntrospection(IdentityServerAuthenticationOptions options)
        {
            var introspectionOptions = new OAuth2IntrospectionOptions
            {
                AuthenticationScheme = options.AuthenticationScheme,
                Authority = options.Authority,
                ScopeName = options.ScopeName,
                ScopeSecret = options.ScopeSecret,

                AutomaticAuthenticate = options.AutomaticAuthenticate,
                AutomaticChallenge = options.AutomaticChallenge,

                NameClaimType = options.NameClaimType,
                RoleClaimType = options.RoleClaimType,

                TokenRetriever = _tokenRetriever,
                SaveTokensAsClaims = options.SaveTokensAsClaims,

                DiscoveryTimeout = options.BackChannelTimeouts,
                IntrospectionTimeout = options.BackChannelTimeouts
            };

            if (options.IntrospectionBackChannelHandler != null)
            {
                introspectionOptions.IntrospectionHttpHandler = options.IntrospectionBackChannelHandler;
            }
            if (options.IntrospectionDiscoveryHandler != null)
            {
                introspectionOptions.DiscoveryHttpHandler = options.IntrospectionDiscoveryHandler;
            }

            return introspectionOptions;
        }