Esempio n. 1
0
        public static IApplicationBuilder UseBearerAuthentication(this IApplicationBuilder builder)
        {
            var validator = new BearerTokenValidator((IApiKeyProvider)builder.ApplicationServices.GetService(typeof(IApiKeyProvider)));

            //
            // Options
            var options = new JwtBearerOptions()
            {
                AutomaticAuthenticate = true,
                AutomaticChallenge    = true,
                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));
                    }
                }
            };

            options.SecurityTokenValidators.Add(validator);

            return(builder.UseJwtBearerAuthentication(options));
        }
        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));
        }