using Elight.Logic; using Elight.Utility; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Authorization; using Microsoft.AspNetCore.Mvc.Controllers; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection.Extensions; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using SqlSugar; using System.Data; using System.Text; using System.Text.Json.Serialization; using System.Text.Json; using Microsoft.AspNetCore.Http.Json; using _24Hour.Controllers.Common; #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 = "My 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.AddMvc(config => { config.Filters.Add(new AllowAnonymousFilter()); }); // 配置跨域 //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(); //})); // 添加身份验证服务 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(); //jwt 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; }); #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", "My API V1"); c.RoutePrefix = string.Empty; }); #endregion app.Run(); #endregion