95 lines
3.0 KiB
C#
95 lines
3.0 KiB
C#
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.IdentityModel.Tokens;
|
|
using Serilog;
|
|
using System.IdentityModel.Tokens.Jwt;
|
|
using System.Security.Claims;
|
|
using YAPDatabase;
|
|
using YAPWebAPI;
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
// Add services to the container.
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
using var loggerFactory = new LoggerFactory();
|
|
loggerFactory.AddSerilog(new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger());
|
|
builder.Services.AddSingleton(loggerFactory.CreateLogger("Any"));
|
|
|
|
builder.Services.AddAuthorization();
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
.AddJwtBearer(options =>
|
|
{
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
{
|
|
// óêàçûâàåò, áóäåò ëè âàëèäèðîâàòüñÿ èçäàòåëü ïðè âàëèäàöèè òîêåíà
|
|
ValidateIssuer = true,
|
|
// ñòðîêà, ïðåäñòàâëÿþùàÿ èçäàòåëÿ
|
|
ValidIssuer = AuthOptions.ISSUER,
|
|
// áóäåò ëè âàëèäèðîâàòüñÿ ïîòðåáèòåëü òîêåíà
|
|
ValidateAudience = true,
|
|
// óñòàíîâêà ïîòðåáèòåëÿ òîêåíà
|
|
ValidAudience = AuthOptions.AUDIENCE,
|
|
// áóäåò ëè âàëèäèðîâàòüñÿ âðåìÿ ñóùåñòâîâàíèÿ
|
|
ValidateLifetime = true,
|
|
// óñòàíîâêà êëþ÷à áåçîïàñíîñòè
|
|
IssuerSigningKey = AuthOptions.GetSymmetricSecurityKey(),
|
|
// âàëèäàöèÿ êëþ÷à áåçîïàñíîñòè
|
|
ValidateIssuerSigningKey = true,
|
|
};
|
|
// â ñëó÷àå îøèáêè àóòåíòèôèêàöèè
|
|
options.Events = new JwtBearerEvents
|
|
{
|
|
OnAuthenticationFailed = context =>
|
|
{
|
|
Console.WriteLine("KEY BYTES (VALIDATE): " + string.Join(",", AuthOptions.GetSymmetricSecurityKey().Key));
|
|
Console.WriteLine("JWT Error: " + context.Exception.Message);
|
|
return Task.CompletedTask;
|
|
}
|
|
};
|
|
});
|
|
|
|
|
|
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
|
builder.Services.AddOpenApi();
|
|
|
|
var app = builder.Build();
|
|
|
|
// Configure the HTTP request pipeline.
|
|
if (app.Environment.IsDevelopment())
|
|
{
|
|
app.MapOpenApi();
|
|
}
|
|
|
|
if (app.Environment.IsProduction())
|
|
{
|
|
var dbContext = app.Services.GetRequiredService<YAPDbContext>();
|
|
if (dbContext.Database.CanConnect())
|
|
{
|
|
dbContext.Database.EnsureCreated();
|
|
dbContext.Database.Migrate();
|
|
}
|
|
}
|
|
|
|
app.UseHttpsRedirection();
|
|
app.UseAuthentication();
|
|
app.UseAuthorization();
|
|
|
|
|
|
app.Map("/login/{username}", (string username) =>
|
|
{
|
|
Console.WriteLine("KEY BYTES (CREATE): " + string.Join(",", AuthOptions.GetSymmetricSecurityKey().Key));
|
|
return new JwtSecurityTokenHandler().WriteToken(new JwtSecurityToken(
|
|
issuer: AuthOptions.ISSUER,
|
|
audience: AuthOptions.AUDIENCE,
|
|
claims: [new(ClaimTypes.Name, username)],
|
|
expires: DateTime.UtcNow.Add(TimeSpan.FromMinutes(2)),
|
|
signingCredentials: new SigningCredentials(AuthOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256)));
|
|
});
|
|
|
|
app.MapControllers();
|
|
|
|
app.Run();
|
|
|