Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
            services.AddSingleton<UserService>();
            services.AddSingleton<Services.TokenHandler>();
            services.AddScoped<AuthenticationService>();

            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(jwtBearerOptions =>
                {
                    var tokenOptions = TokenOptions.GetDefault();
                    var signingConfigurations = new SigningConfigurations(tokenOptions.Secret);

                    jwtBearerOptions.TokenValidationParameters = new TokenValidationParameters()
                    {
                        ValidateAudience = true,
                        ValidateLifetime = true,
                        ValidateIssuerSigningKey = true,
                        ValidIssuer = tokenOptions.Issuer,
                        ValidAudience = tokenOptions.Audience,
                        IssuerSigningKey = signingConfigurations.SecurityKey,
                        ClockSkew = TimeSpan.Zero
                    };
                });
        }
Example #2
0
 public TokenHandler()
 {
     _tokenOptions            = TokenOptions.GetDefault();
     _signingConfigurations   = new SigningConfigurations(_tokenOptions.Secret);
     _jwtSecurityTokenHandler = new JwtSecurityTokenHandler();
 }