Created
July 21, 2019 08:32
-
-
Save ntakouris/91befbac1f39a26e09dc46a5e0a2fcd8 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