Startup.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using EasyMemoryCache;
  2. using Microsoft.AspNetCore.Authentication.JwtBearer;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.EntityFrameworkCore;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.DependencyInjection;
  9. using Microsoft.Extensions.Hosting;
  10. using Microsoft.Extensions.Logging;
  11. using Microsoft.OpenApi.Models;
  12. using Quartz;
  13. using Quartz.Impl;
  14. using Sugar.Enties;
  15. using System;
  16. using System.IO;
  17. using System.Reflection;
  18. using Utils;
  19. using Utils.Jwt;
  20. namespace PMS.NetCore
  21. {
  22. public class Startup
  23. {
  24. private IWebHostEnvironment _env;
  25. private const string CONTENTROOTPATHTOKEN = "%CONTENTROOTPATH%";
  26. public Startup(IConfiguration configuration, IWebHostEnvironment env)
  27. {
  28. Configuration = configuration;
  29. _env = env;
  30. }
  31. public IConfiguration Configuration { get; }
  32. // This method gets called by the runtime. Use this method to add services to the container.
  33. public void ConfigureServices(IServiceCollection services)
  34. {
  35. var signingConfigurations = new SigningConfigurations();
  36. services.AddSingleton(signingConfigurations);
  37. services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();
  38. // TODO create options classes for app settings
  39. var identityConn = AppSettingsHelper.Configuration["AppSettings:ConnectionString"];
  40. //identityConn = identityConn.Replace(CONTENTROOTPATHTOKEN, _env.ContentRootPath);
  41. services.AddDbContext<IdentityAuthDbContext>(options => options.UseSqlServer(identityConn));
  42. services.AddDefaultIdentity<AppUser>().AddEntityFrameworkStores<IdentityAuthDbContext>();
  43. services.AddMemoryCache();
  44. #region jwt配置
  45. services.AddTransient<ITokenHelper, TokenHelper>();
  46. //读取配置文件配置的jwt相关配置
  47. services.Configure<JWTConfig>(Configuration.GetSection("JWTConfig"));
  48. //启用JWT
  49. services.AddAuthentication(Options =>
  50. {
  51. Options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
  52. Options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
  53. }).
  54. AddJwtBearer();
  55. services.AddScoped<Filter.TokenAuthorize>();
  56. #endregion
  57. services.AddLogging(log =>
  58. {
  59. log.AddConsole();
  60. log.AddDebug();
  61. });
  62. services.AddAuthorization(auth =>
  63. {
  64. auth.AddPolicy("Bearer", new AuthorizationPolicyBuilder()
  65. .AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme)
  66. .RequireAuthenticatedUser().Build());
  67. });
  68. services.AddMvcCore()
  69. .AddApiExplorer();
  70. services.AddHttpClient();
  71. services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
  72. {
  73. builder.AllowAnyOrigin()
  74. .AllowAnyMethod()
  75. .AllowAnyHeader();
  76. }));
  77. services.Configure<AppSettings>(Configuration.GetSection("AppSettings"));
  78. services.AddSingleton<IConfiguration>(Configuration);
  79. services.AddSingleton<ICaching, Caching>();
  80. services.AddSwaggerGen(c =>
  81. {
  82. c.SwaggerDoc("v1", new OpenApiInfo { Title = "PMS.NetCore API——Net5.0", Version = "v1" });
  83. // 为 Swagger 设置xml文档注释路径
  84. var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
  85. var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
  86. // 添加控制器层注释,true表示显示控制器注释
  87. c.IncludeXmlComments(xmlPath, true);
  88. });
  89. services.AddControllers();
  90. }
  91. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  92. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  93. {
  94. var isIIS = Convert.ToBoolean(AppSettingsHelper.Configuration["isIIS"]);
  95. var virtualPath = AppSettingsHelper.Configuration["virtualPath"];
  96. var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger();
  97. if (env.IsDevelopment())
  98. {
  99. app.UseDeveloperExceptionPage();
  100. }
  101. app.UseHttpsRedirection();
  102. app.UseRouting();
  103. app.UseAuthorization();
  104. app.UseEndpoints(endpoints =>
  105. {
  106. endpoints.MapControllers();
  107. });
  108. app.UseSwagger();
  109. if (isIIS == false) //若不是IIS部署
  110. {
  111. logger.Info($"isIIS:{isIIS}");
  112. app.UseSwaggerUI(c =>
  113. {
  114. c.SwaggerEndpoint("/swagger/v1/swagger.json", "PMS.NetCore v1");
  115. });
  116. }
  117. else //若是IIS部署,并且是虚拟目录部署
  118. {
  119. logger.Info($"isIIS:{isIIS}");
  120. app.UseSwaggerUI(c =>
  121. {
  122. c.SwaggerEndpoint($"{virtualPath}/swagger/v1/swagger.json", "PMS.NetCore v1");
  123. });
  124. }
  125. }
  126. }
  127. }