using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.Extensions.FileProviders; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using NLog.Web; using Shentun.PeisReport.Api.Jwt; using System.IdentityModel.Tokens.Jwt; using System.Text; var builder = WebApplication.CreateBuilder(args); var configuration = builder.Configuration; // Add services to the container. //nlog builder.Logging.AddNLog("Configs/NLog.config"); builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(opt => { var baseDirectory = AppContext.BaseDirectory; var commentsFile = Path.Combine(baseDirectory, "Shentun.PeisReport.Api.xml"); opt.IncludeXmlComments(commentsFile, true); }); var apiGroupNames = configuration["ApiGroupName"]; builder.Services.AddSwaggerGen(c => { if (apiGroupNames.Contains("JianLiApi")) { c.SwaggerDoc("JianLiApi", new OpenApiInfo { Title = "监利医院", Version = "V1", Description = "监利医院" }); } if (apiGroupNames.Contains("QingHaiWuYuanApi")) { c.SwaggerDoc("QingHaiWuYuanApi", new OpenApiInfo { Title = "青海五院", Version = "V1", Description = "青海五院" }); } var baseDirectory = AppContext.BaseDirectory; //中文注释 var commentsFile = Path.Combine(baseDirectory, "Shentun.PeisReport.Api.xml"); c.IncludeXmlComments(commentsFile, true); }); builder.Services.AddCors(options => { options.AddPolicy("AllowAll", builder => builder .AllowAnyOrigin() .AllowAnyMethod() .AllowAnyHeader()); }); var jwtHelper = new JwtHelper(); //添加JWT 认证服务 builder.Services.AddAuthentication(opt => { opt.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; opt.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(opt => { opt.TokenValidationParameters = jwtHelper.GetTokenValidationParameters(); }); var app = builder.Build(); var isSwagger = Convert.ToBoolean(configuration["Swagger:IsEnabled"]); if (isSwagger) { app.UseSwagger(); //app.UseSwaggerUI(); app.UseSwaggerUI(c => { if (apiGroupNames.Contains("JianLiApi")) c.SwaggerEndpoint($"/swagger/JianLiApi/swagger.json", "监利医院Api"); //分组显示 if (apiGroupNames.Contains("QingHaiWuYuanApi")) c.SwaggerEndpoint($"/swagger/QingHaiWuYuanApi/swagger.json", "青海五院Api"); //分组显示 //c.DefaultModelExpandDepth(-1); }); } //虚拟目录 app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(configuration["ReportVirtualPath:RealPath"]), RequestPath = configuration["ReportVirtualPath:RequestPath"] }); app.UseStaticFiles(new StaticFileOptions { FileProvider = new PhysicalFileProvider(configuration["LisReportVirtualPath:RealPath"]), RequestPath = configuration["LisReportVirtualPath:RequestPath"] }); app.UseCors("AllowAll"); app.UseAuthentication(); app.UseAuthorization(); app.MapControllers(); app.Run();