Startup.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using IdentityServer4.Stores;
  2. using Microsoft.AspNetCore.Builder;
  3. using Microsoft.AspNetCore.Hosting;
  4. using Microsoft.AspNetCore.Server.Kestrel.Core;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.DependencyInjection;
  7. using Microsoft.Extensions.Hosting;
  8. using Microsoft.OpenApi.Models;
  9. using QM.AuthServer.Auth;
  10. using System;
  11. using System.IO;
  12. using System.Reflection;
  13. using System.Security.Cryptography;
  14. using WS;
  15. using WS.Cache;
  16. using WS.Ico;
  17. using WS.Swagger;
  18. using WS.Swagger.Builder;
  19. using WS.Swagger.Filters;
  20. using WS.WebCore.Filter;
  21. namespace QM.AuthServer
  22. {
  23. public class Startup
  24. {
  25. public Startup(IConfiguration configuration)
  26. {
  27. Configuration = configuration;
  28. }
  29. public IConfiguration Configuration { get; }
  30. // This method gets called by the runtime. Use this method to add services to the container.
  31. public void ConfigureServices(IServiceCollection services)
  32. {
  33. // 缓存注入
  34. services.AddScoped<ICacheService, MemoryCacheService>();
  35. services.AddControllers();
  36. //services.AddSwaggerGen(c =>
  37. //{
  38. // c.SwaggerDoc("v1", new OpenApiInfo { Title = "QM.AuthServer", Version = "v1" });
  39. //});
  40. services.AddAutoDIService();
  41. #region Session
  42. //Session 保存到内存 (10分钟)
  43. services.AddDistributedMemoryCache();
  44. services.AddSession(options =>
  45. {
  46. //options.IdleTimeout = TimeSpan.FromMinutes(int.Parse(Configuration["Authorize:Session:Expires"]));
  47. options.Cookie.HttpOnly = true;
  48. });
  49. #endregion
  50. #region Swagger UI Service
  51. var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
  52. //注册Swagger生成器,定义一个和多个Swagger 文档
  53. //services.AddSwaggerGen(CustsomSwaggerOptions);
  54. services.AddCustomSwagger(CURRENT_SWAGGER_OPTIONS);
  55. // If using Kestrel:
  56. services.Configure<KestrelServerOptions>(options => { options.AllowSynchronousIO = true; });
  57. // If using IIS:
  58. services.Configure<IISServerOptions>(options => { options.AllowSynchronousIO = true; });
  59. #endregion
  60. #region IdentityServer4
  61. //RSA:证书长度2048以上,否则抛异常
  62. //配置AccessToken的加密证书
  63. var rsa = new RSACryptoServiceProvider();
  64. //从配置文件获取加密证书
  65. rsa.ImportCspBlob(Convert.FromBase64String(Configuration["Auth:SigningCredential"]));
  66. var section = Configuration.GetSection("SSOConfig");
  67. //客户端模式--怎么执行Ids4
  68. services.AddIdentityServer()//怎么处理
  69. .AddDeveloperSigningCredential()//默认开发者证书-生产环境更换为使用AddSigningCredential()
  70. //.AddSigningCredential(new RsaSecurityKey(rsa)) //设置加密证书
  71. .AddInMemoryCaching()
  72. .AddInMemoryIdentityResources(InitConfig.GetIdentityResources())
  73. .AddInMemoryApiScopes(InitConfig.GetApiScopes(section))//配置资源
  74. .AddInMemoryApiResources(InitConfig.GetApiResources(section))//配置资源
  75. .AddInMemoryClients(InitConfig.GetClients(section))//配置客户端
  76. .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>()
  77. .AddProfileService<ProfileService>();
  78. #endregion
  79. services.AddMvc(options =>
  80. {
  81. options.Filters.Add(typeof(GlobalExceptionsFilter));
  82. });
  83. }
  84. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  85. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  86. {
  87. if (env.IsDevelopment())
  88. {
  89. app.UseDeveloperExceptionPage();
  90. //app.UseSwagger();
  91. //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "QM.AuthServer v1"));
  92. }
  93. app.UseIdentityServer();//添加认证中间件
  94. app.UseHttpsRedirection();
  95. //启用Session
  96. app.UseSession();
  97. app.UseRouting();
  98. app.UseAuthorization();
  99. #region Swagger UI
  100. //CURRENT_SWAGGER_OPTIONS.ApiVersions = provider.ApiVersionDescriptions.Select(s => s.GroupName).ToArray();
  101. app.UseSwagger();
  102. app.UseCustomSwagger(CURRENT_SWAGGER_OPTIONS);
  103. #endregion
  104. app.UseStaticFiles();
  105. app.UseEndpoints(endpoints =>
  106. {
  107. endpoints.MapControllers();
  108. });
  109. }
  110. #region Swagger 项目接口文档配置
  111. /// <summary>
  112. /// 项目接口文档配置
  113. /// </summary>
  114. private CustsomSwaggerOptions CURRENT_SWAGGER_OPTIONS = new CustsomSwaggerOptions()
  115. {
  116. ProjectName = ProConst.PRO_TITLE,
  117. AppName = ProConst.APP_NAME,
  118. AddSwaggerGenAction = c =>
  119. {
  120. c.OperationFilter<AssignOperationVendorFilter>();
  121. var filePath = Path.Combine(AppContext.BaseDirectory, typeof(Program).GetTypeInfo().Assembly.GetName().Name + ".xml");
  122. //controller及action注释
  123. c.IncludeXmlComments(filePath, true);
  124. },
  125. UseSwaggerAction = c =>
  126. {
  127. },
  128. UseSwaggerUIAction = c =>
  129. {
  130. }
  131. };
  132. #endregion
  133. }
  134. }