public static void AddAuthenticationEngine(this IServiceCollection services, IConfigurationSection authConfigurationSection)
        {
            AuthenticationConfig authenticationConfig = authConfigurationSection.Get <AuthenticationConfig>();
            Authenticator        authenticator        = new Authenticator(authenticationConfig);

            services.AddSingleton <AuthenticationConfig>(authenticationConfig);
            services.AddSingleton <Authenticator>(authenticator);

            services.AddAuthentication(delegate(AuthenticationOptions authOptions)
            {
                authOptions.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                authOptions.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(authenticator.SetBearerTokenOptions);
        }
Esempio n. 2
0
        public void SetBearerTokenOptions(JwtBearerOptions jwtOptions, AuthenticationConfig authenticationConfig)
        {
            jwtOptions.RequireHttpsMetadata = false;
            jwtOptions.SaveToken            = true;

            jwtOptions.TokenValidationParameters = new TokenValidationParameters
            {
                ValidateIssuerSigningKey = !authenticationConfig.IgnoreSignature,
                IssuerSigningKey         = this.Key,
                ValidateIssuer           = !String.IsNullOrEmpty(this.config.Issuer),
                ValidIssuer      = this.config.Issuer,
                ValidateAudience = !String.IsNullOrEmpty(this.config.Audience),
                ValidAudience    = this.config.Audience,
                ValidateLifetime = authenticationConfig.TokenExpireTimeInSeconds > 0,
                TokenReader      = new TokenReader(delegate(string token, TokenValidationParameters validationParameters)
                {
                    this.TokenData = (JwtSecurityToken) new JwtSecurityTokenHandler().ReadToken(token);
                    return(this.TokenData);
                }),
            };
        }
Esempio n. 3
0
 public Authenticator(AuthenticationConfig config)
 {
     this.config  = config;
     this.lazyKey = new Lazy <SecurityKey>(this.GenerateSecurityKey);
 }