using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Hosting; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi; using Quartz; using Quartz.Impl; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; using VOL.Core.CacheManager; using VOL.Core.Configuration; using VOL.Core.Controllers.Basic; using VOL.Core.Dapper; using VOL.Core.Extensions; using VOL.Core.Filters; using VOL.Core.Middleware; using VOL.Core.ObjectActionValidator; using VOL.Core.Quartz; using VOL.WebApi; using VOL.WebApi.Controllers.Hubs; var builder = WebApplication.CreateBuilder(args); builder.Services.AddModule(builder.Configuration); builder.Services .AddControllers() .AddCompatibility() .AddNewtonsoftJson(op => { op.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver(); op.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss"; op.SerializerSettings.Converters.Add(new LongCovert()); //op.SerializerSettings.Converters.Add(new StringCovert()); }); DapperParseGuidTypeHandler.InitParseGuid(); builder.Services.AddAuthentication(options => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { SaveSigninToken = true,//保存token,后台验证token是否生效(重要) ValidateIssuer = true,//是否验证Issuer ValidateAudience = true,//是否验证Audience ValidateLifetime = true,//是否验证失效时间 ValidateIssuerSigningKey = true,//是否验证SecurityKey ValidAudience = AppSetting.Secret.Audience,//Audience ValidIssuer = AppSetting.Secret.Issuer,//Issuer,这两项和前面签发jwt的设置一致 IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(AppSetting.Secret.JWT)) }; options.Events = new JwtBearerEvents() { OnChallenge = context => { context.HandleResponse(); context.Response.Clear(); context.Response.ContentType = "application/json"; context.Response.StatusCode = 401; context.Response.WriteAsync(new { message = "授权未通过", status = false, code = 401 }.Serialize()); return Task.CompletedTask; } }; }); builder.Services.AddCors(options => { options.AddPolicy("cors", builder => { builder.SetIsOriginAllowed(_ => true) .AllowAnyMethod() .AllowAnyHeader() .AllowCredentials(); }); }); const string bearerSchemeId = "Bearer"; builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "vol.core.api", Version = "v1" }); c.AddSecurityDefinition(bearerSchemeId, new OpenApiSecurityScheme { Description = "JWT授权token前面需要加上字段Bearer与一个空格,如Bearer token", Type = SecuritySchemeType.Http, Scheme = "bearer", BearerFormat = "JWT" }); c.AddSecurityRequirement(document => new OpenApiSecurityRequirement { [new OpenApiSecuritySchemeReference(bearerSchemeId, document)] = [] }); }); builder.Services.AddSignalR(); builder.Services.AddHttpClient() .AddHttpContextAccessor() .AddMemoryCache() .AddTransient() .AddSingleton() .AddSingleton() .AddSingleton(); builder.Services.AddMvc(options => { options.Filters.Add(typeof(ApiAuthorizeFilter)); options.Filters.Add(typeof(ActionExecuteFilter)); }); var startup = new Startup(builder.Configuration); builder.Services.UseMethodsModelParameters().UseMethodsGeneralParameters(); builder.Services.AddSingleton(new NullObjectModelValidator()); //Swagger builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); //后台默认启动端口 builder.WebHost.UseUrls("http://*:9991"); builder.Services.Configure(x => { x.MultipartBodyLengthLimit = 1024 * 1024 * 100; }).Configure(options => { options.Limits.MaxRequestBodySize = 1024 * 1024 * 100; }).Configure(options => { options.MaxRequestBodySize = 1024 * 1024 * 100; }); var app = builder.Build(); //正式环境如果要关闭swgger,请注释下面三行代码 //app.UseDeveloperExceptionPage(); //app.UseSwagger(); //app.UseSwaggerUI(); if (app.Environment.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseSwagger(); app.UseSwaggerUI(); } else { //定时任务,如果需要本地执行定时任务,请将此代码放在else外面 app.UseQuartz(app.Environment); } app.UseMiddleware(); app.UseDefaultFiles(); app.UseStaticFiles().UseStaticFiles(new StaticFileOptions { ServeUnknownFileTypes = true }); app.Use(HttpRequestMiddleware.Context); string _uploadPath = (app.Environment.ContentRootPath + "/Upload").ReplacePath(); if (!Directory.Exists(_uploadPath)) { Directory.CreateDirectory(_uploadPath); } app.UseStaticFiles(new StaticFileOptions() { FileProvider = new PhysicalFileProvider( Path.Combine(Directory.GetCurrentDirectory(), @"Upload")), RequestPath = "/Upload", OnPrepareResponse = (Microsoft.AspNetCore.StaticFiles.StaticFileResponseContext staticFile) => { } }); //配置HttpContext app.UseStaticHttpContext(); // Configure the HTTP request pipeline. //正式环境禁用swagger请取消下面if注释 //if (app.Environment.IsDevelopment()) //{ app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"); options.RoutePrefix = string.Empty; }); //} app.UseCors("cors"); app.UseCors(); // 使用路由 app.UseRouting(); app.UseAuthorization(); app.MapControllers(); app.MapHub("/message"); app.Run();