using Elight.Logic; using Elight.Utility; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using SqlSugar; using System.Text; using _24Hour.Controllers.Common; using System.IdentityModel.Tokens.Jwt; using Quartz.Impl; using Quartz; using Elight.Logic.Job; using Quartz.Spi; using Microsoft.IdentityModel.Logging; using System.Collections.Specialized; using System.Runtime.InteropServices; #region builder var builder = WebApplication.CreateBuilder(args); var Configuration = builder.Configuration; builder.WebHost.UseUrls(Configuration.GetSection("UrlsConfiguration:Urls").Value.Split(",")); // Add services to the container. builder.Services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.PropertyNamingPolicy = null; options.JsonSerializerOptions.Converters.Add(new Elight.Utility.DateTimeNullableConverter()); options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter()); options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter()); options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter()); options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter()); options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter()); options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter()); options.JsonSerializerOptions.Converters.Add(new Elight.Utility.NullableConverter()); }); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); #region Swagger文件 builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "24小时一体机 API", Version = "v1" }); //在接口类、方法标记属性 [HiddenApi],可以阻止【Swagger文档】生成 c.DocumentFilter(); c.OrderActionsBy(o => o.RelativePath); var basePath = System.AppDomain.CurrentDomain.BaseDirectory;//获取应用程序所在目录(绝对,不受工作目录影响,建议采用此方法获取路径) var xmlPath = Path.Combine(basePath, "24Hour.xml"); if (File.Exists(xmlPath))//避免没有该文件时报错 c.IncludeXmlComments(xmlPath, true); //添加Jwt验证设置 c.AddSecurityRequirement(new OpenApiSecurityRequirement() { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Id = "Bearer", Type = ReferenceType.SecurityScheme } }, new List() } }); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "Value: Bearer {token}", Name = "Authorization", In = ParameterLocation.Header, Type = SecuritySchemeType.ApiKey }); }); #endregion // 配置跨域 builder.Services.AddCors(policy => { policy.AddPolicy("CorsPolicy", opt => opt .AllowAnyOrigin() .AllowAnyHeader() .AllowAnyMethod() .WithExposedHeaders("X-Pagination")); }); //builder.Services.AddCors(options => options.AddPolicy("CorsPolicy", //c => //{ // c.WithOrigins(Configuration.GetSection("UrlsConfiguration:CorUrls").Value.Split(',', StringSplitOptions.RemoveEmptyEntries)) // .AllowAnyHeader() // 允许任何标头(这个最好写到AllowAnyMethod上面去) // .AllowAnyMethod() // 允许任何方法访问 // .SetIsOriginAllowed(o => true) // =AllowAnyOrigin() // .AllowCredentials(); //})); //清除jwt toekn JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.Clear(); // 添加身份验证服务 builder.Services.AddAuthentication(options => { options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = nameof(ResponseAuthenticationHandler); //401 options.DefaultForbidScheme = nameof(ResponseAuthenticationHandler); //403 }) .AddJwtBearer(options => { // 配置JWT身份验证选项 options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = Configuration.GetSection("JwtConfiguration:Issuer").Value, ValidAudience = Configuration.GetSection("JwtConfiguration:Audience").Value, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration.GetSection("JwtConfiguration:Jwtkey").Value)), ClockSkew = TimeSpan.Zero }; //重点在于这里;判断是WebSocket的路径(https://www.cnblogs.com/fger/p/11811190.html) options.Events = new JwtBearerEvents { OnMessageReceived = (context) => { if (!context.HttpContext.Request.Path.HasValue) return Task.CompletedTask; //重点在于这里;判断是WebSocket的路径 var accessToken = context.HttpContext.Request.Query["access_token"]; var path = context.HttpContext.Request.Path; if (string.IsNullOrWhiteSpace(accessToken) || !path.StartsWithSegments("/ws")) return Task.CompletedTask; context.Token = accessToken; return Task.CompletedTask; }, //此处为权限验证失败后触发的事件 OnChallenge = context => { //此处代码为终止.Net Core默认的返回类型和数据结果,这个很重要哦,必须 context.HandleResponse(); //自定义自己想要返回的数据结果,我这里要返回的是Json对象,通过引用Newtonsoft.Json库进行转换 var payload = new { StatusCode = 0, Message = "身份认证失败!" }; //自定义返回的数据类型 context.Response.ContentType = "application/json"; //自定义返回状态码,默认为401 我这里改成 200 context.Response.StatusCode = StatusCodes.Status200OK; //context.Response.StatusCode = StatusCodes.Status401Unauthorized; //输出Json数据结果 context.Response.WriteAsync(Convert.ToString(payload)); return Task.FromResult(0); } }; }).AddScheme(nameof(ResponseAuthenticationHandler), o => { }); //builder.Services.AddAuthorization(); builder.Services.AddHttpContextAccessor(); builder.Services.AddScoped(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); //WriteSysLog builder.Services.AddScoped(sp => { var connectionString = Configuration.GetSection("ConnectionStrings:MySQLConnString").Value; var db = new SqlSugarClient(new ConnectionConfig { ConnectionString = connectionString, DbType = SqlSugar.DbType.MySql, IsAutoCloseConnection = true, InitKeyType = InitKeyType.Attribute }); return db; }); #region 定时任务 // 配置Quartz.NET调度器 builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.TryAddSingleton(); builder.Services.AddHostedService(); // 注册Quartz.NET的托管服务 //Task.Factory.StartNew(() => //{ // try // { // while (true) // { // Thread.Sleep(60 * 1000); // ClearMemory(); // } // } // catch (Exception ex) // { // } //}); //Task.Factory.StartNew(() => //{ // RunJob().Wait(); //}); #endregion #endregion #region APP var app = builder.Build(); // Configure the HTTP request pipeline. if (app.Environment.IsDevelopment()) { app.UseSwagger(); app.UseSwaggerUI(); } #region websockets配置 app.UseWebSockets(new WebSocketOptions { KeepAliveInterval = TimeSpan.FromMinutes(2) }); //app.UseMiddleware(); #endregion app.UseStaticFiles(); //app.UseHttpsRedirection(); //路由 app.UseRouting(); app.UseAuthentication(); // 启用身份验证中间件 app.UseAuthorization(); // 启用授权中间件 //app.MapControllers(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); #region swagger app.UseSwagger();// 启用Swagger中间件 app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "24小时一体机 API V1"); c.RoutePrefix = string.Empty; }); #endregion app.UseCors("CorsPolicy"); app.Run(); #endregion #region 定时 static void ClearMemory() { try { GC.Collect(); GC.WaitForPendingFinalizers(); } catch (Exception ex) { } } static async Task RunJob() { try { NameValueCollection props = new NameValueCollection { { "quartz.serializer.type", "binary" } }; StdSchedulerFactory factory = new StdSchedulerFactory(props); IScheduler scheduler = await factory.GetScheduler(); // 启动任务调度器 await scheduler.Start(); #region 通道消息发送每33秒执行3 IJobDetail job3 = JobBuilder.Create() .WithIdentity("job3", SchedulerConstants.DefaultGroup) .Build(); var trigger3 = TriggerBuilder.Create() .WithIdentity("trigger3") // 给任务一个名字 .StartAt(DateTime.Now) // 设置任务开始时间 .ForJob("job3", SchedulerConstants.DefaultGroup) //给任务指定一个分组 .WithCronSchedule("*/20 * * * * ?") .UsingJobData("k", 321) .Build(); await scheduler.ScheduleJob(job3, trigger3); #endregion } catch (SchedulerException se) { await Console.Error.WriteLineAsync(se.ToString()); } } #endregion