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();