Created
July 21, 2019 08:31
-
-
Save ntakouris/04c4df05a06c73743f002fed309c6c4c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var jwtSettings = new JwtSettings(); | |
configuration.Bind(nameof(jwtSettings), jwtSettings); | |
services.AddSingleton(jwtSettings); | |
var tokenValidationParameters = new TokenValidationParameters | |
{ | |
ValidateIssuerSigningKey = true, | |
IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtSettings.Secret)), | |
ValidateIssuer = false, | |
ValidateAudience = false, | |
RequireExpirationTime = false, | |
ValidateLifetime = true | |
}; | |
services.AddSingleton(tokenValidationParameters); | |
services.AddAuthentication(x => | |
{ | |
x.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; | |
x.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; | |
x.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; | |
}) | |
.AddJwtBearer(x => | |
{ | |
x.SaveToken = true; | |
x.TokenValidationParameters = tokenValidationParameters; | |
}); | |
services.AddSwaggerGen(x => | |
{ | |
x.SwaggerDoc("v1", new OpenApiInfo{ Title = "Tweetbook API", Version = "v1" }); | |
x.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme | |
{ | |
Description = "JWT Authorization header using the bearer scheme", | |
Name = "Authorization", | |
In = ParameterLocation.Header, | |
Type = SecuritySchemeType.ApiKey | |
}); | |
x.AddSecurityRequirement(new OpenApiSecurityRequirement | |
{ | |
{new OpenApiSecurityScheme{Reference = new OpenApiReference | |
{ | |
Id = "Bearer", | |
Type = ReferenceType.SecurityScheme | |
}}, new List<string>()} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment