2026-08-13 23:10:22 +07:00
|
|
|
using System.Text;
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
using Microsoft.OpenApi;
|
2026-09-20 16:21:15 +07:00
|
|
|
using mws.backend.dotnet.api.Authentication;
|
2026-08-19 23:25:08 +07:00
|
|
|
using mws.backend.dotnet.api.Middleware;
|
|
|
|
|
using mws.backend.dotnet.application.Auth;
|
2026-09-20 16:21:15 +07:00
|
|
|
using mws.backend.dotnet.application.Common;
|
2026-08-19 23:25:08 +07:00
|
|
|
using mws.backend.dotnet.infrastructure;
|
|
|
|
|
using mws.backend.dotnet.infrastructure.Persistence;
|
2026-08-13 23:10:22 +07:00
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
builder.Services
|
|
|
|
|
.AddControllers()
|
|
|
|
|
.AddJsonOptions(options =>
|
|
|
|
|
{
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(
|
|
|
|
|
new System.Text.Json.Serialization.JsonStringEnumConverter());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSwaggerGen(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SwaggerDoc("v1", new OpenApiInfo
|
|
|
|
|
{
|
|
|
|
|
Title = "MWS API",
|
|
|
|
|
Version = "v1"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.AddSecurityDefinition("bearer", new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
Name = "Authorization",
|
|
|
|
|
Type = SecuritySchemeType.Http,
|
|
|
|
|
Scheme = "bearer",
|
|
|
|
|
BearerFormat = "JWT",
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
Description = "JWT Authorization header using Bearer scheme."
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
options.AddSecurityRequirement(document =>
|
|
|
|
|
new OpenApiSecurityRequirement
|
|
|
|
|
{
|
|
|
|
|
[new OpenApiSecuritySchemeReference("bearer", document)] = []
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddInfrastructure(builder.Configuration);
|
2026-09-20 16:21:15 +07:00
|
|
|
builder.Services.AddHttpContextAccessor();
|
|
|
|
|
builder.Services.AddScoped<ICurrentUser, HttpCurrentUser>();
|
2026-08-13 23:10:22 +07:00
|
|
|
|
|
|
|
|
var jwtSection = builder.Configuration.GetSection("Jwt");
|
|
|
|
|
var jwtSecret = jwtSection["Secret"];
|
|
|
|
|
var jwtIssuer = jwtSection["Issuer"];
|
|
|
|
|
var jwtAudience = jwtSection["Audience"];
|
|
|
|
|
|
|
|
|
|
builder.Services
|
|
|
|
|
.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
|
|
|
|
|
|
|
|
|
ValidIssuer = jwtIssuer,
|
|
|
|
|
ValidAudience = jwtAudience,
|
|
|
|
|
|
|
|
|
|
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSecret))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
options.MapInboundClaims = false;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
|
|
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
var origins = builder.Configuration["Cors:Origins"];
|
|
|
|
|
options.AddPolicy("Frontend", policy =>
|
|
|
|
|
{
|
|
|
|
|
policy
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowAnyMethod()
|
|
|
|
|
.WithOrigins(origins.Split(';', StringSplitOptions.RemoveEmptyEntries));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
|
|
|
|
app.UseApiExceptionMiddleware();
|
|
|
|
|
|
|
|
|
|
using (var scope = app.Services.CreateScope())
|
|
|
|
|
{
|
|
|
|
|
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
|
|
|
|
|
await db.Database.MigrateAsync();
|
|
|
|
|
await DbSeeder.SeedAsync(db, scope.ServiceProvider.GetRequiredService<IPasswordHasher>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI(options =>
|
|
|
|
|
{
|
|
|
|
|
options.SwaggerEndpoint(
|
|
|
|
|
"/swagger/v1/swagger.json",
|
|
|
|
|
"MWS API v1");
|
|
|
|
|
|
|
|
|
|
options.EnablePersistAuthorization();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
|
|
app.UseCors("Frontend");
|
|
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
app.Run();
|