public static IServiceCollection AddBearerAuthentication(this IServiceCollection services, Action <ApiKeyOptions> configure = null)
        {
            var opts = new ApiKeyOptions();

            if (configure != null)
            {
                configure.Invoke(opts);
            }

            IHostingEnvironment env    = services.BuildServiceProvider().GetRequiredService <IHostingEnvironment>();
            IConfiguration      config = services.BuildServiceProvider().GetRequiredService <IConfiguration>();

            var provider = new ApiKeyProvider(opts);

            provider.UseStorage(new ApiKeyFileStorage(env.GetConfigPath("api-keys.json")));
            provider.UseStorage(new ApiKeyConfigStorage(config));

            var validator = new BearerTokenValidator(provider);

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = HttpSysDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options =>
            {
                options.SecurityTokenValidators.Add(validator);

                options.Events = new JwtBearerEvents()
                {
                    OnMessageReceived = ctx =>
                    {
                        validator.OnReceivingToken(ctx);
                        return(System.Threading.Tasks.Task.FromResult(0));
                    },
                    OnTokenValidated = ctx =>
                    {
                        validator.OnValidatedToken(ctx);
                        return(System.Threading.Tasks.Task.FromResult(0));
                    }
                };
            });

            return(services.AddSingleton <IApiKeyProvider>(provider));
        }
        public static IServiceCollection AddApiKeyProvider(this IServiceCollection services, Action <ApiKeyOptions> o = null)
        {
            var options = new ApiKeyOptions();

            if (o != null)
            {
                o.Invoke(options);
            }

            IHostingEnvironment env    = services.BuildServiceProvider().GetRequiredService <IHostingEnvironment>();
            IConfiguration      config = services.BuildServiceProvider().GetRequiredService <IConfiguration>();

            var provider = new ApiKeyProvider(options);

            provider.UseStorage(new ApiKeyFileStorage(env.GetConfigPath("api-keys.json")));
            provider.UseStorage(new ApiKeyConfigStorage(config));

            return(services.AddSingleton <IApiKeyProvider>(provider));
        }