2023-06-09 18:10:38 +08:00
|
|
|
|
using AspNetCoreRateLimit;
|
2022-03-06 14:26:05 +08:00
|
|
|
|
using Infrastructure;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2023-06-09 18:10:38 +08:00
|
|
|
|
using Microsoft.IdentityModel.Tokens;
|
2022-03-06 14:26:05 +08:00
|
|
|
|
using ZR.Admin.WebApi.Extensions;
|
|
|
|
|
|
using ZR.Admin.WebApi.Filters;
|
2023-06-09 18:10:38 +08:00
|
|
|
|
using ZR.Admin.WebApi.Framework;
|
2022-03-06 14:26:05 +08:00
|
|
|
|
using ZR.Admin.WebApi.Hubs;
|
2023-06-09 18:10:38 +08:00
|
|
|
|
using ZR.Admin.WebApi.Middleware;
|
2023-03-08 13:02:38 +08:00
|
|
|
|
using ZR.Common.Cache;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
2022-03-06 14:26:05 +08:00
|
|
|
|
var builder = WebApplication.CreateBuilder(args);
|
|
|
|
|
|
|
|
|
|
|
|
// Add services to the container.
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddControllers();
|
|
|
|
|
|
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
|
|
|
|
|
builder.Services.AddEndpointsApiExplorer();
|
|
|
|
|
|
builder.Services.AddSwaggerGen();
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//注入HttpContextAccessor
|
2022-03-06 14:26:05 +08:00
|
|
|
|
builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
2023-06-08 11:58:09 +08:00
|
|
|
|
// 跨域配置
|
|
|
|
|
|
builder.Services.AddCors(builder.Configuration);
|
2023-03-08 13:02:38 +08:00
|
|
|
|
// 显示logo
|
|
|
|
|
|
builder.Services.AddLogo();
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//注入SignalR实时通讯,默认用json传输
|
2022-03-06 14:26:05 +08:00
|
|
|
|
builder.Services.AddSignalR();
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//消除Error unprotecting the session cookie警告
|
2022-03-06 14:26:05 +08:00
|
|
|
|
builder.Services.AddDataProtection()
|
|
|
|
|
|
.PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//普通验证码
|
2023-03-15 16:00:16 +08:00
|
|
|
|
builder.Services.AddCaptcha(builder.Configuration);
|
2023-05-12 15:08:24 +08:00
|
|
|
|
//IPRatelimit
|
|
|
|
|
|
builder.Services.AddIPRate(builder.Configuration);
|
2022-03-06 14:26:05 +08:00
|
|
|
|
//builder.Services.AddSession();
|
|
|
|
|
|
builder.Services.AddHttpContextAccessor();
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//绑定整个对象到Model上
|
2022-03-06 14:26:05 +08:00
|
|
|
|
builder.Services.Configure<OptionsSetting>(builder.Configuration);
|
|
|
|
|
|
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//jwt 认证
|
2022-03-06 14:26:05 +08:00
|
|
|
|
builder.Services.AddAuthentication(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
|
}).AddCookie()
|
|
|
|
|
|
.AddJwtBearer(o =>
|
|
|
|
|
|
{
|
|
|
|
|
|
o.TokenValidationParameters = JwtUtil.ValidParameters();
|
2023-06-08 11:58:09 +08:00
|
|
|
|
o.Events = new JwtBearerEvents
|
|
|
|
|
|
{
|
|
|
|
|
|
OnAuthenticationFailed = context =>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 如果过期,把过期信息添加到头部
|
|
|
|
|
|
if (context.Exception.GetType() == typeof(SecurityTokenExpiredException))
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Response.Headers.Add("Token-Expired", "true");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2022-03-06 14:26:05 +08:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSingleton(new AppSettings(builder.Configuration));
|
2023-06-01 18:47:11 +08:00
|
|
|
|
builder.Services.AddAppService();
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//开启计划任务
|
2022-03-06 14:26:05 +08:00
|
|
|
|
builder.Services.AddTaskSchedulers();
|
|
|
|
|
|
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//注册REDIS 服务
|
2023-03-08 13:02:38 +08:00
|
|
|
|
var openRedis = builder.Configuration["RedisServer:open"];
|
|
|
|
|
|
if (openRedis == "1")
|
2022-03-06 14:26:05 +08:00
|
|
|
|
{
|
2023-03-08 13:02:38 +08:00
|
|
|
|
RedisServer.Initalize();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-03-06 14:26:05 +08:00
|
|
|
|
builder.Services.AddMvc(options =>
|
|
|
|
|
|
{
|
2022-06-01 17:46:35 +08:00
|
|
|
|
options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
|
2022-03-06 14:26:05 +08:00
|
|
|
|
})
|
|
|
|
|
|
.AddJsonOptions(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
builder.Services.AddSwaggerConfig();
|
|
|
|
|
|
|
|
|
|
|
|
var app = builder.Build();
|
2022-07-09 15:51:59 +08:00
|
|
|
|
InternalApp.ServiceProvider = app.Services;
|
2023-06-20 07:26:25 +08:00
|
|
|
|
InternalApp.Configuration = builder.Configuration;
|
|
|
|
|
|
InternalApp.WebHostEnvironment = app.Environment;
|
2023-06-09 18:10:38 +08:00
|
|
|
|
//初始化db
|
|
|
|
|
|
builder.Services.AddDb(builder.Configuration, app.Environment);
|
2022-07-01 22:39:39 +08:00
|
|
|
|
|
2023-06-13 12:09:32 +08:00
|
|
|
|
//使用全局异常中间件
|
|
|
|
|
|
app.UseMiddleware<GlobalExceptionMiddleware>();
|
2022-05-19 18:34:52 +08:00
|
|
|
|
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//使可以多次多去body内容
|
2022-03-06 14:26:05 +08:00
|
|
|
|
app.Use((context, next) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Request.EnableBuffering();
|
|
|
|
|
|
if (context.Request.Query.TryGetValue("access_token", out var token))
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Request.Headers.Add("Authorization", $"Bearer {token}");
|
|
|
|
|
|
}
|
|
|
|
|
|
return next();
|
|
|
|
|
|
});
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//开启访问静态文件/wwwroot目录文件,要放在UseRouting前面
|
2022-03-06 14:26:05 +08:00
|
|
|
|
app.UseStaticFiles();
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//开启路由访问
|
2022-03-06 14:26:05 +08:00
|
|
|
|
app.UseRouting();
|
2022-06-01 17:46:35 +08:00
|
|
|
|
app.UseCors("Policy");//要放在app.UseEndpoints前。
|
2022-03-06 14:26:05 +08:00
|
|
|
|
//app.UseHttpsRedirection();
|
|
|
|
|
|
|
|
|
|
|
|
app.UseAuthentication();
|
|
|
|
|
|
app.UseAuthorization();
|
|
|
|
|
|
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//开启缓存
|
2022-03-06 14:26:05 +08:00
|
|
|
|
app.UseResponseCaching();
|
2022-06-01 17:46:35 +08:00
|
|
|
|
//恢复/启动任务
|
2022-03-06 14:26:05 +08:00
|
|
|
|
app.UseAddTaskSchedulers();
|
2023-06-13 12:09:32 +08:00
|
|
|
|
//使用swagger
|
|
|
|
|
|
app.UseSwagger();
|
2023-05-12 15:08:24 +08:00
|
|
|
|
//启用客户端IP限制速率
|
|
|
|
|
|
app.UseIpRateLimiting();
|
|
|
|
|
|
app.UseRateLimiter();
|
2022-11-18 13:26:39 +08:00
|
|
|
|
//设置socket连接
|
|
|
|
|
|
app.MapHub<MessageHub>("/msgHub");
|
|
|
|
|
|
|
|
|
|
|
|
app.MapControllerRoute(
|
|
|
|
|
|
name: "default",
|
|
|
|
|
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
|
|
|
2022-03-06 14:26:05 +08:00
|
|
|
|
app.MapControllers();
|
|
|
|
|
|
|
|
|
|
|
|
app.Run();
|