2025-07-06 16:29:18 +08:00
|
|
|
|
using System.Reflection;
|
2025-07-06 18:54:51 +08:00
|
|
|
|
using System.Text;
|
2025-07-08 23:06:19 +08:00
|
|
|
|
using AGSS.DbSet;
|
2025-07-02 18:27:13 +08:00
|
|
|
|
using AGSS.Models;
|
2025-07-08 23:06:19 +08:00
|
|
|
|
using AGSS.Models.Entities;
|
2025-07-04 23:28:24 +08:00
|
|
|
|
using AGSS.Models.Template;
|
2025-07-11 22:59:29 +08:00
|
|
|
|
using AGSS.Services;
|
2025-07-08 23:06:19 +08:00
|
|
|
|
using AGSS.Utilities;
|
2025-07-11 16:17:31 +08:00
|
|
|
|
using asg_form;
|
2025-07-01 14:57:03 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2025-07-08 23:06:19 +08:00
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
2025-07-02 18:27:13 +08:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2025-07-01 14:57:03 +08:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2025-07-06 16:29:18 +08:00
|
|
|
|
using Microsoft.OpenApi.Models;
|
2025-07-01 14:57:03 +08:00
|
|
|
|
|
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
|
var configuration=builder.Configuration;
|
2025-07-01 14:57:03 +08:00
|
|
|
|
var domain = builder.Configuration["Auth0:Domain"];
|
2025-07-08 23:06:19 +08:00
|
|
|
|
var audience = builder.Configuration["Auth0:Audience"];
|
2025-07-01 14:57:03 +08:00
|
|
|
|
|
2025-07-06 16:29:18 +08:00
|
|
|
|
builder.Services.AddCors(options =>
|
|
|
|
|
{
|
|
|
|
|
options.AddPolicy("AllowAll", builder =>
|
|
|
|
|
{
|
|
|
|
|
builder.AllowAnyOrigin() // 允许所有来源
|
|
|
|
|
.AllowAnyHeader()
|
|
|
|
|
.AllowAnyMethod();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
|
// 数据库配置(PGSQL)
|
|
|
|
|
builder.Services.AddDbContext<ApplicationDbContext>(opt =>
|
2025-07-02 18:27:13 +08:00
|
|
|
|
opt.UseNpgsql(builder.Configuration.GetConnectionString("DBContext")));
|
2025-07-01 14:57:03 +08:00
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
|
// Identity 配置
|
2025-07-11 16:17:31 +08:00
|
|
|
|
builder.Services.AddIdentityCore<UserModel>(options =>
|
|
|
|
|
{
|
|
|
|
|
options.Password.RequireDigit = false;
|
|
|
|
|
options.Password.RequireLowercase = false;
|
|
|
|
|
options.Password.RequireNonAlphanumeric = false;
|
|
|
|
|
options.Password.RequireUppercase = false;
|
|
|
|
|
options.Password.RequiredLength = 6;
|
|
|
|
|
options.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider;
|
|
|
|
|
options.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider;
|
|
|
|
|
})
|
|
|
|
|
.AddRoles<RoleModel>()
|
|
|
|
|
.AddEntityFrameworkStores<ApplicationDbContext>()
|
|
|
|
|
.AddDefaultUI()
|
|
|
|
|
;
|
2025-07-06 18:54:51 +08:00
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
|
// 注册 UserService
|
2025-07-11 16:17:31 +08:00
|
|
|
|
// builder.Services.AddScoped<UserService>();
|
2025-07-06 18:54:51 +08:00
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
|
builder.Services.AddScoped<Jwt>();
|
2025-07-11 22:59:29 +08:00
|
|
|
|
builder.Services.AddScoped<UserService>();
|
2025-07-14 15:10:13 +08:00
|
|
|
|
builder.Services.AddScoped<MenuService>();
|
2025-07-11 16:17:31 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.Configure<JWTOptions>(builder.Configuration.GetSection("JWT"));
|
|
|
|
|
|
|
|
|
|
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
|
|
|
|
|
.AddJwtBearer(x =>
|
2025-07-08 23:06:19 +08:00
|
|
|
|
{
|
2025-07-11 16:17:31 +08:00
|
|
|
|
var jwtOpt = builder.Configuration.GetSection("JWT").Get<JWTOptions>();
|
|
|
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(jwtOpt.SigningKey);
|
|
|
|
|
var secKey = new SymmetricSecurityKey(keyBytes);
|
|
|
|
|
x.TokenValidationParameters = new()
|
2025-07-08 23:06:19 +08:00
|
|
|
|
{
|
2025-07-11 16:17:31 +08:00
|
|
|
|
ValidateIssuer = false,
|
|
|
|
|
ValidateAudience = false,
|
2025-07-08 23:06:19 +08:00
|
|
|
|
ValidateLifetime = true,
|
|
|
|
|
ValidateIssuerSigningKey = true,
|
2025-07-11 16:17:31 +08:00
|
|
|
|
IssuerSigningKey = secKey
|
2025-07-08 23:06:19 +08:00
|
|
|
|
};
|
2025-07-11 16:17:31 +08:00
|
|
|
|
})
|
2025-07-11 22:59:29 +08:00
|
|
|
|
.AddCookie("Identity.External").AddCookie("Identity.Application");
|
2025-07-04 23:28:24 +08:00
|
|
|
|
|
2025-07-01 14:57:03 +08:00
|
|
|
|
|
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
|
builder.Services.AddAuthorization();
|
2025-07-01 14:57:03 +08:00
|
|
|
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
2025-07-08 23:06:19 +08:00
|
|
|
|
|
2025-07-06 16:29:18 +08:00
|
|
|
|
builder.Services.AddSwaggerGen(c =>
|
|
|
|
|
{
|
2025-07-08 23:06:19 +08:00
|
|
|
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "ZeroNode后端文档", Version = "1.0.0", Description = "使用了Dotnet9.0,数据库采用PGSql,作者:罗澜,7000" });
|
|
|
|
|
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
|
|
|
|
|
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
|
2025-07-06 16:29:18 +08:00
|
|
|
|
c.IncludeXmlComments(xmlPath);
|
2025-07-06 18:54:51 +08:00
|
|
|
|
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
|
|
|
|
|
{
|
|
|
|
|
In = ParameterLocation.Header,
|
|
|
|
|
Type = SecuritySchemeType.ApiKey,
|
|
|
|
|
Description = "直接在下框中输入Bearer {token}(注意两者之间是一个空格)",
|
|
|
|
|
Name = "Authorization",
|
|
|
|
|
BearerFormat = "JWT",
|
|
|
|
|
Scheme = "Bearer"
|
|
|
|
|
});
|
|
|
|
|
c.AddSecurityRequirement(new OpenApiSecurityRequirement
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new OpenApiSecurityScheme
|
|
|
|
|
{
|
2025-07-08 23:06:19 +08:00
|
|
|
|
Reference = new OpenApiReference
|
2025-07-06 18:54:51 +08:00
|
|
|
|
{
|
2025-07-08 23:06:19 +08:00
|
|
|
|
Type = ReferenceType.SecurityScheme,
|
|
|
|
|
Id = "Bearer"
|
2025-07-06 18:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
},
|
2025-07-08 23:06:19 +08:00
|
|
|
|
new string[] { }
|
2025-07-06 18:54:51 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
2025-07-06 16:29:18 +08:00
|
|
|
|
});
|
2025-07-01 14:57:03 +08:00
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
|
|
|
|
|
2025-07-06 16:29:18 +08:00
|
|
|
|
app.UseCors("AllowAll");
|
|
|
|
|
|
2025-07-04 23:28:24 +08:00
|
|
|
|
// 配置Swagger
|
2025-07-06 16:29:18 +08:00
|
|
|
|
app.UseSwagger();
|
|
|
|
|
app.UseSwaggerUI();
|
2025-07-01 14:57:03 +08:00
|
|
|
|
|
|
|
|
|
app.UseHttpsRedirection();
|
2025-07-08 23:06:19 +08:00
|
|
|
|
app.UseStaticFiles();
|
|
|
|
|
app.UseRouting();
|
2025-07-01 14:57:03 +08:00
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
|
// 自定义中间件 把404变成200
|
2025-07-04 23:28:24 +08:00
|
|
|
|
app.Use(async (context, next) =>
|
|
|
|
|
{
|
|
|
|
|
await next(); // 先执行后续中间件
|
2025-07-08 23:06:19 +08:00
|
|
|
|
|
2025-07-04 23:28:24 +08:00
|
|
|
|
// 如果响应是 404 且未修改过
|
|
|
|
|
if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
|
|
|
|
|
{
|
|
|
|
|
context.Response.StatusCode = 200; // 改为 200
|
|
|
|
|
context.Response.ContentType = "application/json";
|
2025-07-08 23:06:19 +08:00
|
|
|
|
|
2025-07-04 23:28:24 +08:00
|
|
|
|
// 自定义响应内容
|
2025-07-08 23:06:19 +08:00
|
|
|
|
await context.Response.WriteAsJsonAsync(new ReturnTemplate(404, "未能找到资源吖!", null!));
|
2025-07-04 23:28:24 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
|
// 控制器路由
|
2025-07-01 14:57:03 +08:00
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
2025-07-08 23:06:19 +08:00
|
|
|
|
app.MapRazorPages();
|
|
|
|
|
|
|
|
|
|
app.Run();
|