2022-04-04 18:53:02 +08:00
|
|
|
|
using AspNetCoreRateLimit;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using Hei.Captcha;
|
|
|
|
|
|
using Infrastructure;
|
|
|
|
|
|
using Infrastructure.Extensions;
|
2021-12-03 17:42:44 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2021-10-11 18:05:01 +08:00
|
|
|
|
using System;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using System.IO;
|
2022-01-01 16:05:06 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using ZR.Admin.WebApi.Extensions;
|
|
|
|
|
|
using ZR.Admin.WebApi.Filters;
|
2021-11-23 09:55:37 +08:00
|
|
|
|
using ZR.Admin.WebApi.Framework;
|
2022-02-27 21:11:46 +08:00
|
|
|
|
using ZR.Admin.WebApi.Hubs;
|
2021-08-23 16:57:25 +08:00
|
|
|
|
using ZR.Admin.WebApi.Middleware;
|
|
|
|
|
|
|
|
|
|
|
|
namespace ZR.Admin.WebApi
|
|
|
|
|
|
{
|
|
|
|
|
|
public class Startup
|
|
|
|
|
|
{
|
2021-12-26 18:26:38 +08:00
|
|
|
|
public Startup(IConfiguration configuration)
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
Configuration = configuration;
|
|
|
|
|
|
}
|
2021-12-06 12:54:53 +08:00
|
|
|
|
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
|
2021-08-23 16:57:25 +08:00
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
|
|
public void ConfigureServices(IServiceCollection services)
|
|
|
|
|
|
{
|
2022-03-03 20:55:37 +08:00
|
|
|
|
string corsUrls = Configuration["corsUrls"];
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//配置跨域
|
2021-08-23 16:57:25 +08:00
|
|
|
|
services.AddCors(c =>
|
|
|
|
|
|
{
|
|
|
|
|
|
c.AddPolicy("Policy", policy =>
|
|
|
|
|
|
{
|
2021-12-01 14:06:01 +08:00
|
|
|
|
policy.WithOrigins(corsUrls.Split(',', StringSplitOptions.RemoveEmptyEntries))
|
2022-06-01 17:25:41 +08:00
|
|
|
|
.AllowAnyHeader()//允许任意头
|
|
|
|
|
|
.AllowCredentials()//允许cookie
|
|
|
|
|
|
.AllowAnyMethod();//允许任意方法
|
2021-08-23 16:57:25 +08:00
|
|
|
|
});
|
|
|
|
|
|
});
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//注入SignalR实时通讯,默认用json传输
|
2022-02-27 21:11:46 +08:00
|
|
|
|
services.AddSignalR(options =>
|
|
|
|
|
|
{
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//客户端发保持连接请求到服务端最长间隔,默认30秒,改成4分钟,网页需跟着设置connection.keepAliveIntervalInMilliseconds = 12e4;即2分钟
|
2022-02-27 21:11:46 +08:00
|
|
|
|
//options.ClientTimeoutInterval = TimeSpan.FromMinutes(4);
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//服务端发保持连接请求到客户端间隔,默认15秒,改成2分钟,网页需跟着设置connection.serverTimeoutInMilliseconds = 24e4;即4分钟
|
2022-02-27 21:11:46 +08:00
|
|
|
|
//options.KeepAliveInterval = TimeSpan.FromMinutes(2);
|
|
|
|
|
|
});
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//消除Error unprotecting the session cookie警告
|
2022-02-23 18:30:17 +08:00
|
|
|
|
services.AddDataProtection()
|
|
|
|
|
|
.PersistKeysToFileSystem(new DirectoryInfo(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "DataProtection"));
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//普通验证码
|
2021-08-23 16:57:25 +08:00
|
|
|
|
services.AddHeiCaptcha();
|
2022-04-04 18:53:02 +08:00
|
|
|
|
services.AddIPRate(Configuration);
|
2021-08-23 16:57:25 +08:00
|
|
|
|
services.AddSession();
|
2022-04-04 18:53:02 +08:00
|
|
|
|
services.AddMemoryCache();
|
2021-08-23 16:57:25 +08:00
|
|
|
|
services.AddHttpContextAccessor();
|
|
|
|
|
|
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//绑定整个对象到Model上
|
2021-08-23 16:57:25 +08:00
|
|
|
|
services.Configure<OptionsSetting>(Configuration);
|
2021-12-03 17:42:44 +08:00
|
|
|
|
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//jwt 认证
|
2021-12-03 17:42:44 +08:00
|
|
|
|
services.AddAuthentication(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
|
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
|
|
|
}).AddCookie()
|
|
|
|
|
|
.AddJwtBearer(o =>
|
|
|
|
|
|
{
|
|
|
|
|
|
o.TokenValidationParameters = JwtUtil.ValidParameters();
|
|
|
|
|
|
});
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
2021-12-26 18:26:38 +08:00
|
|
|
|
InjectServices(services, Configuration);
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
|
|
|
|
|
services.AddMvc(options =>
|
|
|
|
|
|
{
|
2022-06-01 17:25:41 +08:00
|
|
|
|
options.Filters.Add(typeof(GlobalActionMonitor));//全局注册
|
2021-08-23 16:57:25 +08:00
|
|
|
|
})
|
2021-11-23 09:55:37 +08:00
|
|
|
|
.AddJsonOptions(options =>
|
|
|
|
|
|
{
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeConverter());
|
|
|
|
|
|
options.JsonSerializerOptions.Converters.Add(new JsonConverterUtil.DateTimeNullConverter());
|
|
|
|
|
|
});
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
2021-12-04 10:26:38 +08:00
|
|
|
|
services.AddSwaggerConfig();
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
|
|
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (env.IsDevelopment())
|
|
|
|
|
|
{
|
|
|
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
|
|
|
}
|
2021-12-22 15:57:42 +08:00
|
|
|
|
app.UseSwagger();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//使可以多次多去body内容
|
2021-08-23 16:57:25 +08:00
|
|
|
|
app.Use((context, next) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Request.EnableBuffering();
|
2022-02-28 18:37:23 +08:00
|
|
|
|
if (context.Request.Query.TryGetValue("access_token", out var token))
|
|
|
|
|
|
{
|
|
|
|
|
|
context.Request.Headers.Add("Authorization", $"Bearer {token}");
|
|
|
|
|
|
}
|
2021-08-23 16:57:25 +08:00
|
|
|
|
return next();
|
|
|
|
|
|
});
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//开启访问静态文件/wwwroot目录文件,要放在UseRouting前面
|
2021-08-23 16:57:25 +08:00
|
|
|
|
app.UseStaticFiles();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//开启路由访问
|
2021-08-23 16:57:25 +08:00
|
|
|
|
app.UseRouting();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
app.UseCors("Policy");//要放在app.UseEndpoints前。
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//app.UseAuthentication会启用Authentication中间件,该中间件会根据当前Http请求中的Cookie信息来设置HttpContext.User属性(后面会用到),
|
|
|
|
|
|
//所以只有在app.UseAuthentication方法之后注册的中间件才能够从HttpContext.User中读取到值,
|
|
|
|
|
|
//这也是为什么上面强调app.UseAuthentication方法一定要放在下面的app.UseMvc方法前面,因为只有这样ASP.NET Core的MVC中间件中才能读取到HttpContext.User的值。
|
|
|
|
|
|
//1.先开启认证
|
2021-08-23 16:57:25 +08:00
|
|
|
|
app.UseAuthentication();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//2.再开启授权
|
2021-08-23 16:57:25 +08:00
|
|
|
|
app.UseAuthorization();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//开启session
|
2022-04-09 19:11:16 +08:00
|
|
|
|
//app.UseSession();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//开启缓存
|
2021-08-23 16:57:25 +08:00
|
|
|
|
app.UseResponseCaching();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//恢复/启动任务
|
2021-08-23 16:57:25 +08:00
|
|
|
|
app.UseAddTaskSchedulers();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//使用全局异常中间件
|
2021-08-23 16:57:25 +08:00
|
|
|
|
app.UseMiddleware<GlobalExceptionMiddleware>();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//启用客户端IP限制速率
|
2022-04-04 18:53:02 +08:00
|
|
|
|
app.UseIpRateLimiting();
|
2021-08-23 16:57:25 +08:00
|
|
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
|
|
|
|
{
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//设置socket连接
|
2022-02-28 18:37:23 +08:00
|
|
|
|
endpoints.MapHub<MessageHub>("/msgHub");
|
|
|
|
|
|
|
2021-08-23 16:57:25 +08:00
|
|
|
|
endpoints.MapControllerRoute(
|
|
|
|
|
|
name: "default",
|
|
|
|
|
|
pattern: "{controller=Home}/{action=Index}/{id?}");
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2022-06-01 17:25:41 +08:00
|
|
|
|
/// 注册Services服务
|
2021-08-23 16:57:25 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="services"></param>
|
2021-12-26 18:26:38 +08:00
|
|
|
|
/// <param name="configuration"></param>
|
|
|
|
|
|
private void InjectServices(IServiceCollection services, IConfiguration configuration)
|
2021-08-23 16:57:25 +08:00
|
|
|
|
{
|
|
|
|
|
|
services.AddAppService();
|
2022-02-23 18:30:17 +08:00
|
|
|
|
services.AddSingleton(new AppSettings(configuration));
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//开启计划任务
|
2021-08-23 16:57:25 +08:00
|
|
|
|
services.AddTaskSchedulers();
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//初始化db
|
2021-12-26 18:26:38 +08:00
|
|
|
|
DbExtension.AddDb(configuration);
|
2022-02-23 18:30:17 +08:00
|
|
|
|
|
2022-06-01 17:25:41 +08:00
|
|
|
|
//注册REDIS 服务
|
2022-01-01 16:05:06 +08:00
|
|
|
|
Task.Run(() =>
|
|
|
|
|
|
{
|
2022-01-02 10:48:27 +08:00
|
|
|
|
//RedisServer.Initalize();
|
2022-01-01 16:05:06 +08:00
|
|
|
|
});
|
2021-08-23 16:57:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|