Method that configures Jwt authentication:
// Configure authentication with JWT (Json Web Token). public void ConfigureJwtAuthService(IServiceCollection services) { // Enable the use of an [Authorize(AuthenticationSchemes = // JwtBearerDefaults.AuthenticationScheme)] // attribute on methods and classes to protect. services.AddAuthentication().AddJwtBearer(cfg => { cfg.RequireHttpsMetadata = false; cfg.SaveToken = true; cfg.TokenValidationParameters = new TokenValidationParameters() { IssuerSigningKey = JwtController.SecurityKey, ValidAudience = JwtController.Audience, ValidIssuer = JwtController.Issuer, // When receiving a token, check that we've signed it. ValidateIssuerSigningKey = true, // When receiving a token, check that it is still valid. ValidateLifetime = true, // This defines the maximum allowable clock skew when validating // the lifetime. As we're creating the tokens locally and validating // them on the same machines which should have synchronised time, // this can be set to zero. ClockSkew = TimeSpan.FromMinutes(0) }; }); }
Now, using the ConfigureServices () method for Startup.cs, you can call the ConfigureJwtAuthService () method to configure Jwt authentication.
source share