Exemple #1
0
        public static AuthenticationBuilder AddJwtBearerAuthentication(this IServiceCollection services, Action <JwtTokenOptions> options)
        {
            var optInstance = new JwtTokenOptions();

            options(optInstance);

            services.AddSingleton(optInstance);
            services.AddTransient <IJwtTokenService, JwtTokenService>();

            return(services.AddAuthentication(o =>
            {
                o.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                o.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
                o.DefaultSignInScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(cfg =>
            {
                cfg.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateAudience = optInstance.ValidateAudience,
                    ValidateIssuer = optInstance.ValidateIssuer,
                    ValidateIssuerSigningKey = optInstance.ValidateIssuerSigningKey,
                    ValidateLifetime = optInstance.ValidateLifeTime,
                    IssuerSigningKey = optInstance.SecurityKey
                };
            }));
        }
Exemple #2
0
 public JwtTokenService(JwtTokenOptions options)
 {
     _options = options;
 }