Beispiel #1
0
        public static IServiceCollection AddJwtAuthentication(this IServiceCollection services, JwtSettings settings)
        {
            services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                var key = Encoding.UTF8.GetBytes(settings.SecretKey);

                options.TokenValidationParameters = new TokenValidationParameters
                {
                    RequireExpirationTime    = true,
                    ValidateLifetime         = true,
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = new SymmetricSecurityKey(key)
                };
            });

            return(services);
        }
Beispiel #2
0
 public static IServiceCollection AddDefaultJwtManager(this IServiceCollection services, JwtSettings settings)
 {
     return(services.AddTransient <IJwtManager>(_ => new DefaultJwtManager(settings.SecretKey, settings.TokenTtl)));
 }