PIAPS_CW/WebApp/Program.cs

58 lines
1.6 KiB
C#
Raw Normal View History

2024-06-19 22:32:09 +04:00
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using WebApp;
2024-05-09 17:45:03 +04:00
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
2024-06-19 22:32:09 +04:00
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
var secretKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["SecretKey"]));
options.TokenValidationParameters = new()
{
ValidateLifetime = true,
IssuerSigningKey = secretKey,
ValidateIssuer = false,
ValidateAudience = false,
};
options.Events = new JwtBearerEvents()
{
OnMessageReceived = context =>
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD> JWT <20><><EFBFBD><EFBFBD><EFBFBD>
context.Token = context.Request.Cookies["21gunsthebest"];
return Task.CompletedTask;
}
};
});
builder.Services.AddAuthorization();
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> API
APIClient.Connect(builder.Configuration);
2024-05-09 17:45:03 +04:00
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
2024-06-19 22:32:09 +04:00
app.UseAuthentication();
2024-05-09 17:45:03 +04:00
app.UseAuthorization();
app.MapRazorPages();
2024-06-19 22:32:09 +04:00
app.Run();