Example #1
0
        private void ConfigureAuth(IServiceCollection services)
        {
            var tokenConfig             = new TokenAuthConfig();
            var tokenConfigFromSettings = Configuration.GetSection("tokenConfig");

            tokenConfigFromSettings.Bind(tokenConfig);

            var tokenManager = new AuthTokenManager(tokenConfig);

            services.AddSingleton(tokenManager);

            services.AddAuthentication(options => {
                options.DefaultAuthenticateScheme = "JwtBearer";
                options.DefaultChallengeScheme    = "JwtBearer";
            })
            .AddJwtBearer("JwtBearer", jwtBearerOptions =>
            {
                jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = tokenManager.Key,
                    ValidateIssuer           = false,
                    ValidateAudience         = false,
                    ValidateLifetime         = true,    //validate the expiration and not before values in the token
                    ClockSkew = TimeSpan.FromMinutes(5) //5 minute tolerance for the expiration date
                };
            });
        }
Example #2
0
 public AuthTokenManager(TokenAuthConfig secrets)
 {
     mTokenGenConfig = secrets;
     mSecretTokenKey = CreateAuthenticationSecret(secrets.TokenSecret);
 }