From a2e38d2ba9edee2987ba3f13ba8377c1eb45b76a Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Sat, 7 Sep 2024 13:59:48 +0300 Subject: [PATCH] Actually added auth --- Program.cs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/Program.cs b/Program.cs index 1d1fbe2..b3b9701 100644 --- a/Program.cs +++ b/Program.cs @@ -1,3 +1,7 @@ +using System.Text; +using Microsoft.AspNetCore.Authentication.JwtBearer; +using Microsoft.IdentityModel.Tokens; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -14,6 +18,28 @@ if (app.Environment.IsDevelopment()) 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.Run();