authorization

This commit is contained in:
Aleksandr Popov
2023-06-25 12:39:49 +02:00
parent 4df16b26db
commit 944712df97
6 changed files with 98 additions and 1 deletions
+23
View File
@@ -1,4 +1,5 @@
using System.Reflection;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using Newtonsoft.Json.Converters;
@@ -9,6 +10,7 @@ namespace TURN.Services.Chess
public void ConfigureServices(IServiceCollection services)
{
ConfigureSwagger(services);
ConfigureAuthorization(services);
_ = services.AddControllers();
_ = services.AddControllers().AddNewtonsoftJson(jsonOptions =>
{
@@ -21,6 +23,8 @@ namespace TURN.Services.Chess
public void Configure(IApplicationBuilder app)
{
_ = app.UseRouting();
_ = app.UseAuthentication();
_ = app.UseAuthorization();
_ = app.UseSwagger();
_ = app.UseSwaggerUI();
_ = app.UseEndpoints(endpoints =>
@@ -47,5 +51,24 @@ namespace TURN.Services.Chess
});
_ = services.AddSwaggerGenNewtonsoftSupport();
}
private void ConfigureAuthorization(IServiceCollection services)
{
_ = services.AddAuthentication()
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = AuthOptions.ISSUER,
ValidateAudience = true,
ValidAudience = AuthOptions.AUDIENCE,
ValidateLifetime = true,
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
ValidateIssuerSigningKey = true,
};
});
_ = services.AddAuthorization();
}
}
}