Actually added auth

This commit is contained in:
Nikolai Papin 2024-09-07 13:59:48 +03:00
parent 1afb436c9c
commit a2e38d2ba9

View File

@ -1,3 +1,7 @@
using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
// Add services to the container. // Add services to the container.
@ -14,6 +18,28 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI(); app.UseSwaggerUI();
} }
// Authorization
builder.Services.AddAuthorization();
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
string issuer = Environment.GetEnvironmentVariable("JWT_ISSUER") ?? "renis";
string audience = Environment.GetEnvironmentVariable("JWT_AUDIENCE") ?? "renis";
string secret = Environment.GetEnvironmentVariable("JWT_SECRET") ?? "TopSecretKeyForTheProtectionOfChocolateCookiesAndOtherSweetThings";
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidIssuer = issuer,
ValidateAudience = true,
ValidAudience = audience,
ValidateLifetime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(secret)),
ValidateIssuerSigningKey = true
};
});
app.UseHttpsRedirection(); app.UseHttpsRedirection();
app.Run(); app.Run();