2025-07-02 18:27:13 +08:00
|
|
|
|
using AGSS.Models;
|
2025-07-04 23:28:24 +08:00
|
|
|
|
using AGSS.Models.Template;
|
2025-07-01 14:57:03 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2025-07-02 18:27:13 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-07-01 14:57:03 +08:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var domain = builder.Configuration["Auth0:Domain"];
|
|
|
|
|
var audience =builder.Configuration["Auth0:Audience"];
|
|
|
|
|
|
2025-07-04 23:28:24 +08:00
|
|
|
|
//数据库配置(PGSQL)
|
2025-07-02 18:27:13 +08:00
|
|
|
|
builder.Services.AddDbContext<DBContext>(opt =>
|
|
|
|
|
opt.UseNpgsql(builder.Configuration.GetConnectionString("DBContext")));
|
2025-07-01 14:57:03 +08:00
|
|
|
|
|
2025-07-04 23:28:24 +08:00
|
|
|
|
//鉴权配置
|
2025-07-01 14:57:03 +08:00
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.AddJwtBearer(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Authority = domain;
|
|
|
|
|
options.Audience = audience;
|
|
|
|
|
options.TokenValidationParameters = new TokenValidationParameters
|
|
|
|
|
{
|
|
|
|
|
ValidateIssuer = true,
|
|
|
|
|
ValidateAudience = true,
|
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ValidIssuer = domain,
|
|
|
|
|
ValidAudience = audience
|
|
|
|
|
};
|
2025-07-04 23:28:24 +08:00
|
|
|
|
options.Events = new JwtBearerEvents
|
|
|
|
|
{
|
|
|
|
|
OnChallenge = context =>
|
|
|
|
|
{
|
|
|
|
|
context.HandleResponse();
|
|
|
|
|
context.Response.StatusCode = 200;
|
|
|
|
|
context.Response.ContentType = "application/json";
|
|
|
|
|
return context.Response.WriteAsJsonAsync(new ReturnTemplate(401,"你提供了一个错误的Token,所以我们无法验证你的身份,唔......",null));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
2025-07-01 14:57:03 +08:00
|
|
|
|
});
|
|
|
|
|
builder.Services.AddAuthorization();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
builder.Services.AddSwaggerGen();
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2025-07-04 23:28:24 +08:00
|
|
|
|
// 配置Swagger
|
2025-07-01 14:57:03 +08:00
|
|
|
|
if (app.Environment.IsDevelopment())
|
|
|
|
|
{
|
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
2025-07-04 23:28:24 +08:00
|
|
|
|
//自定义中间件 把404变成200
|
|
|
|
|
app.Use(async (context, next) =>
|
|
|
|
|
{
|
|
|
|
|
await next(); // 先执行后续中间件
|
|
|
|
|
|
|
|
|
|
// 如果响应是 404 且未修改过
|
|
|
|
|
if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
|
|
|
|
|
{
|
|
|
|
|
context.Response.StatusCode = 200; // 改为 200
|
|
|
|
|
context.Response.ContentType = "application/json";
|
|
|
|
|
|
|
|
|
|
// 自定义响应内容
|
|
|
|
|
await context.Response.WriteAsJsonAsync(new ReturnTemplate(404,"未能找到资源吖!",null));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
//控制器路由
|
2025-07-01 14:57:03 +08:00
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
app.Run();
|