using Entities.Contracts;
using Microsoft.EntityFrameworkCore;
using TechHelper.Context;
using TechHelper.Repository;
using SharedDATA.Api;
using Entities.Configuration;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using System.Text;
using TechHelper.Features;
using TechHelper.Services;
using TechHelper.Server.Services;
using TechHelper.Server.Repositories;
using Microsoft.OpenApi.Models;
///
/// TechHelper 服务器应用程序的主入口点
/// 配置和启动 ASP.NET Core Web API 应用程序
///
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers(); // 添加 MVC 控制器服务 (用于 API)
// 2. 数据库服务 (DbContext)
builder.Services.AddDbContext(options =>
options.UseMySql(
builder.Configuration.GetConnectionString("XSDB"),
ServerVersion.AutoDetect(builder.Configuration.GetConnectionString("XSDB"))
)
).AddUnitOfWork()
.AddCustomRepository()
.AddCustomRepository()
.AddCustomRepository()
.AddCustomRepository()
.AddCustomRepository()
.AddCustomRepository()
.AddCustomRepository()
.AddCustomRepository()
.AddCustomRepository()
.AddCustomRepository()
.AddCustomRepository();
builder.Services.AddAutoMapper(typeof(AutoMapperProFile).Assembly);
// 3. 配置服务 (IOptions)
builder.Services.Configure(builder.Configuration.GetSection("ApiConfiguration"));
builder.Services.Configure(builder.Configuration.GetSection("JWTSettings"));
// 4. 认证和授权服务 (Identity, JWT, 自定义 Auth)
// 添加 ASP.NET Core Identity (包含默认的 Cookie 认证和授权服务)
builder.Services.AddIdentity>(opt =>
{
opt.User.AllowedUserNameCharacters = "";
opt.Lockout.AllowedForNewUsers = true;
opt.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(2);
opt.Lockout.MaxFailedAccessAttempts = 3;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
builder.Services.Configure(Options =>
{
Options.TokenLifespan = TimeSpan.FromHours(2);
});
// 添加 JWT Bearer 认证方案
var jwtSettings = builder.Configuration.GetSection("JWTSettings");
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; // 设置默认认证方案为 JWT Bearer
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; // 设置默认挑战方案为 JWT Bearer
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true, // 验证签发人
ValidateAudience = true, // 验证受众
ValidateLifetime = true, // 验证令牌有效期
ValidateIssuerSigningKey = true, // 验证签名密钥
ValidIssuer = jwtSettings["validIssuer"], // 合法的签发人
ValidAudience = jwtSettings["validAudience"], // 合法的受众
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings["securityKey"])) // 签名密钥
};
});
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddTransient();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddScoped();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "Your API Name", Version = "v1" });
c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
{
Name = "Authorization",
Type = SecuritySchemeType.Http,
Scheme = "bearer",
BearerFormat = "JWT",
In = ParameterLocation.Header,
Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
});
c.AddSecurityRequirement(new OpenApiSecurityRequirement
{
{
new OpenApiSecurityScheme
{
Reference = new OpenApiReference
{
Type = ReferenceType.SecurityScheme,
Id = "Bearer"
}
},
new string[] {}
}
});
});
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder => builder
.WithOrigins("https://localhost:7047", "http://localhost:7047", "https://localhost:5001", "http://localhost:5001")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials());
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (/*app.Environment.IsDevelopment()*/true)
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseCors("AllowSpecificOrigin");
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();