Пример #1
0
        public static void AddTokenAuthentication(this IServiceCollection services, TokenSettings settings)
        {
            TokenManager mngr = new TokenManager(settings);

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            });

            services.AddJwtBearerAuthentication(o =>
            {
                o.TokenValidationParameters = new TokenValidationParameters()
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = mngr.GetSecurityKey(),

                    ValidateIssuer = true,
                    ValidIssuer    = settings.Issuer,

                    ValidateAudience = true,
                    ValidAudience    = settings.Audience,

                    ValidateLifetime = true,

                    ClockSkew = TimeSpan.Zero
                };
            });
        }
Пример #2
0
        public TokenManager(TokenSettings settings)
        {
            this.settings = settings;

            handler = new JwtSecurityTokenHandler();
        }