123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using IdentityServer4.Stores;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Server.Kestrel.Core;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using Microsoft.OpenApi.Models;
- using QM.AuthServer.Auth;
- using System;
- using System.IO;
- using System.Reflection;
- using System.Security.Cryptography;
- using WS;
- using WS.Cache;
- using WS.Ico;
- using WS.Swagger;
- using WS.Swagger.Builder;
- using WS.Swagger.Filters;
- using WS.WebCore.Filter;
- namespace QM.AuthServer
- {
- public class Startup
- {
- public Startup(IConfiguration configuration)
- {
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- // 缓存注入
- services.AddScoped<ICacheService, MemoryCacheService>();
- services.AddControllers();
- //services.AddSwaggerGen(c =>
- //{
- // c.SwaggerDoc("v1", new OpenApiInfo { Title = "QM.AuthServer", Version = "v1" });
- //});
- services.AddAutoDIService();
- #region Session
- //Session 保存到内存 (10分钟)
- services.AddDistributedMemoryCache();
- services.AddSession(options =>
- {
- //options.IdleTimeout = TimeSpan.FromMinutes(int.Parse(Configuration["Authorize:Session:Expires"]));
- options.Cookie.HttpOnly = true;
- });
- #endregion
- #region Swagger UI Service
- var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
- //注册Swagger生成器,定义一个和多个Swagger 文档
- //services.AddSwaggerGen(CustsomSwaggerOptions);
- services.AddCustomSwagger(CURRENT_SWAGGER_OPTIONS);
- // If using Kestrel:
- services.Configure<KestrelServerOptions>(options => { options.AllowSynchronousIO = true; });
- // If using IIS:
- services.Configure<IISServerOptions>(options => { options.AllowSynchronousIO = true; });
- #endregion
- #region IdentityServer4
- //RSA:证书长度2048以上,否则抛异常
- //配置AccessToken的加密证书
- var rsa = new RSACryptoServiceProvider();
- //从配置文件获取加密证书
- rsa.ImportCspBlob(Convert.FromBase64String(Configuration["Auth:SigningCredential"]));
- var section = Configuration.GetSection("SSOConfig");
- //客户端模式--怎么执行Ids4
- services.AddIdentityServer()//怎么处理
- .AddDeveloperSigningCredential()//默认开发者证书-生产环境更换为使用AddSigningCredential()
- //.AddSigningCredential(new RsaSecurityKey(rsa)) //设置加密证书
- .AddInMemoryCaching()
- .AddInMemoryIdentityResources(InitConfig.GetIdentityResources())
- .AddInMemoryApiScopes(InitConfig.GetApiScopes(section))//配置资源
- .AddInMemoryApiResources(InitConfig.GetApiResources(section))//配置资源
- .AddInMemoryClients(InitConfig.GetClients(section))//配置客户端
- .AddResourceOwnerValidator<ResourceOwnerPasswordValidator>()
- .AddProfileService<ProfileService>();
- #endregion
- services.AddMvc(options =>
- {
- options.Filters.Add(typeof(GlobalExceptionsFilter));
- });
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- //app.UseSwagger();
- //app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "QM.AuthServer v1"));
- }
- app.UseIdentityServer();//添加认证中间件
- app.UseHttpsRedirection();
- //启用Session
- app.UseSession();
- app.UseRouting();
- app.UseAuthorization();
- #region Swagger UI
- //CURRENT_SWAGGER_OPTIONS.ApiVersions = provider.ApiVersionDescriptions.Select(s => s.GroupName).ToArray();
- app.UseSwagger();
- app.UseCustomSwagger(CURRENT_SWAGGER_OPTIONS);
- #endregion
- app.UseStaticFiles();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllers();
- });
- }
- #region Swagger 项目接口文档配置
- /// <summary>
- /// 项目接口文档配置
- /// </summary>
- private CustsomSwaggerOptions CURRENT_SWAGGER_OPTIONS = new CustsomSwaggerOptions()
- {
- ProjectName = ProConst.PRO_TITLE,
- AppName = ProConst.APP_NAME,
- AddSwaggerGenAction = c =>
- {
- c.OperationFilter<AssignOperationVendorFilter>();
- var filePath = Path.Combine(AppContext.BaseDirectory, typeof(Program).GetTypeInfo().Assembly.GetName().Name + ".xml");
- //controller及action注释
- c.IncludeXmlComments(filePath, true);
- },
- UseSwaggerAction = c =>
- {
- },
- UseSwaggerUIAction = c =>
- {
- }
- };
- #endregion
- }
- }
|