Startup.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Security.Claims;
  5. using System.Text;
  6. using System.Text.Encodings.Web;
  7. using System.Text.Unicode;
  8. using HaBookCms.Common.LogHelper;
  9. using HaBookCms.ContextConfig.Exceptions;
  10. using HaBookCms.Jwt.Filter;
  11. using HaBookCms.Jwt.Model;
  12. using HaBookCms.RedisStorage.Cache;
  13. using log4net;
  14. using log4net.Config;
  15. using log4net.Repository;
  16. using Microsoft.AspNetCore.Authentication.Cookies;
  17. using Microsoft.AspNetCore.Authentication.JwtBearer;
  18. using Microsoft.AspNetCore.Builder;
  19. using Microsoft.AspNetCore.Hosting;
  20. using Microsoft.AspNetCore.Http;
  21. using Microsoft.AspNetCore.HttpOverrides;
  22. using Microsoft.AspNetCore.Mvc;
  23. using Microsoft.Extensions.Configuration;
  24. using Microsoft.Extensions.DependencyInjection;
  25. using Microsoft.Extensions.Logging;
  26. using Microsoft.IdentityModel.Tokens;
  27. namespace HaBookCms.Admin
  28. {
  29. public class Startup
  30. {
  31. /// <summary>
  32. /// log4net 仓储库
  33. /// </summary>
  34. public static ILoggerRepository repository { get; set; }
  35. public Startup(IConfiguration configuration, IHostingEnvironment env)
  36. {
  37. Configuration = configuration;
  38. var builder = new ConfigurationBuilder()
  39. .SetBasePath(env.ContentRootPath)
  40. .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
  41. this.Configuration = builder.Build();
  42. //log4net
  43. repository = LogManager.CreateRepository("HaBookCms");
  44. //指定配置文件
  45. XmlConfigurator.Configure(repository, new FileInfo("Log4net.config"));
  46. BaseConfigModel.SetBaseConfig(Configuration, env.ContentRootPath, env.WebRootPath);
  47. }
  48. public IConfiguration Configuration { get; }
  49. // This method gets called by the runtime. Use this method to add services to the container.
  50. public void ConfigureServices(IServiceCollection services)
  51. {
  52. services.Configure<CookiePolicyOptions>(options =>
  53. {
  54. // This lambda determines whether user consent for non-essential cookies is needed for a given request.
  55. options.CheckConsentNeeded = context => true;
  56. options.MinimumSameSitePolicy = SameSiteMode.None;
  57. });
  58. services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  59. //解决视图输出内容中文编码问题
  60. services.AddSingleton(HtmlEncoder.Create(UnicodeRanges.All));
  61. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  62. #region 认证
  63. JwtAuthConfigModel jwtConfig = new JwtAuthConfigModel();
  64. // services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
  65. //.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, o =>
  66. // {
  67. // o.LoginPath = new PathString("/api/Users/checkLogin");
  68. // })
  69. services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  70. .AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, o =>
  71. {
  72. o.TokenValidationParameters = new TokenValidationParameters
  73. {
  74. ValidIssuer = jwtConfig.Issuer,
  75. ValidAudience = jwtConfig.Audience,
  76. IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtConfig.JWTSecretKey)),
  77. /***********************************TokenValidationParameters的参数默认值***********************************/
  78. RequireSignedTokens = true,
  79. // SaveSigninToken = false,
  80. // ValidateActor = false,
  81. // 将下面两个参数设置为false,可以不验证Issuer和Audience,但是不建议这样做。
  82. ValidateAudience = true,
  83. ValidateIssuer = true,
  84. ValidateIssuerSigningKey = true,
  85. // 是否要求Token的Claims中必须包含 Expires
  86. RequireExpirationTime = true,
  87. // 允许的服务器时间偏移量
  88. // ClockSkew = TimeSpan.FromSeconds(300),
  89. ClockSkew = TimeSpan.Zero,
  90. // 是否验证Token有效期,使用当前时间与Token的Claims中的NotBefore和Expires对比
  91. ValidateLifetime = true
  92. };
  93. });
  94. #endregion
  95. var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(jwtConfig.JWTSecretKey)), SecurityAlgorithms.HmacSha256);
  96. // 如果要数据库动态绑定,这里先留个空,后边处理器里动态赋值
  97. var permission = new List<Permission>();
  98. // 角色与接口的权限要求参数
  99. var permissionRequirement = new PermissionRequirement(
  100. "/api/denied",// 拒绝授权的跳转地址(目前无用)
  101. permission,
  102. ClaimTypes.Role,//基于角色的授权
  103. jwtConfig.Issuer,//发行人
  104. jwtConfig.Audience,//听众
  105. signingCredentials,//签名凭据
  106. expiration: TimeSpan.FromSeconds(60 * 2)//接口的过期时间
  107. );
  108. #region 授权
  109. services.AddAuthorization(options =>
  110. {
  111. options.AddPolicy("RequireApp", policy => policy.RequireRole("App").Build());
  112. options.AddPolicy("RequireAdmin", policy => policy.RequireRole("Admin").Build());
  113. options.AddPolicy("RequireAdminOrApp", policy => policy.RequireRole("Admin,App").Build());
  114. // 自定义权限要求
  115. options.AddPolicy("Permission",
  116. policy => policy.Requirements.Add(permissionRequirement));
  117. });
  118. #endregion
  119. //log日志注入
  120. services.AddSingleton<ILoggerHelper, LogHelper>();
  121. #region 缓存 读取配置是否使用哪种缓存模式
  122. services.AddMemoryCache();
  123. if (Convert.ToBoolean(Configuration["Cache:IsUseRedis"]))
  124. {
  125. services.AddSingleton<ICacheService, RedisCacheService>();
  126. }
  127. else
  128. {
  129. services.AddSingleton<ICacheService, MemoryCacheService>();
  130. }
  131. #endregion
  132. #region 缓存 RedisCache
  133. //将Redis分布式缓存服务添加到服务中
  134. services.AddDistributedRedisCache(options =>
  135. {
  136. //用于连接Redis的配置
  137. options.Configuration = "localhost";// Configuration.GetConnectionString("RedisConnectionString");
  138. //Redis实例名RedisDistributedCache
  139. options.InstanceName = "RedisInstance";
  140. });
  141. #endregion
  142. #region CORS
  143. services.AddCors(c =>
  144. {
  145. c.AddPolicy("Any", policy =>
  146. {
  147. policy.AllowAnyOrigin()
  148. .AllowAnyMethod()
  149. .AllowAnyHeader()
  150. .AllowCredentials();
  151. });
  152. c.AddPolicy("Limit", policy =>
  153. {
  154. policy
  155. .WithOrigins("localhost:63969")
  156. .WithMethods("get", "post", "put", "delete")
  157. //.WithHeaders("Authorization");
  158. .AllowAnyHeader();
  159. });
  160. });
  161. #endregion
  162. #region 性能 压缩
  163. services.AddResponseCompression();
  164. #endregion
  165. }
  166. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  167. public void Configure(IApplicationBuilder app, IHostingEnvironment env)
  168. {
  169. if (env.IsDevelopment())
  170. {
  171. app.UseDeveloperExceptionPage();
  172. }
  173. else
  174. {
  175. app.UseExceptionHandler("/Home/Error");
  176. // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
  177. app.UseHsts();
  178. }
  179. #region 解决Ubuntu Nginx 代理不能获取IP问题
  180. app.UseForwardedHeaders(new ForwardedHeadersOptions
  181. {
  182. ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
  183. });
  184. #endregion
  185. app.UseHttpsRedirection();
  186. app.UseStaticFiles(new StaticFileOptions
  187. {
  188. ServeUnknownFileTypes = true
  189. //ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  190. //{
  191. // { ".apk","application/vnd.android.package-archive"},
  192. // { ".nupkg","application/zip"}
  193. //}) //支持特殊文件下载处理
  194. });
  195. //自定义异常处理
  196. app.UseMiddleware<ExceptionFilter>();
  197. //认证
  198. app.UseAuthentication();
  199. //授权
  200. app.UseMiddleware<JwtAuthorizationFilter>();
  201. //性能压缩
  202. app.UseResponseCompression();
  203. app.UseCookiePolicy();
  204. app.UseMvc(routes =>
  205. {
  206. routes.MapRoute(
  207. name: "default",
  208. template: "{controller=Home}/{action=Index}/{id?}");
  209. });
  210. }
  211. }
  212. }