Startup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ServiceModel;
  4. using Microsoft.AspNetCore.Builder;
  5. using Microsoft.AspNetCore.Hosting;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Http.Features;
  8. using Microsoft.AspNetCore.Rewrite;
  9. using Microsoft.AspNetCore.StaticFiles;
  10. using Microsoft.Extensions.Configuration;
  11. using Microsoft.Extensions.DependencyInjection;
  12. using Microsoft.Extensions.FileProviders;
  13. using Microsoft.Extensions.Hosting;
  14. using System.Text;
  15. using Microsoft.IdentityModel.Tokens;
  16. using System.Linq;
  17. using Microsoft.AspNetCore.Server.Kestrel.Core;
  18. namespace MySystem
  19. {
  20. public class Startup
  21. {
  22. public Startup(IConfiguration configuration)
  23. {
  24. Configuration = configuration;
  25. }
  26. public IConfiguration Configuration { get; }
  27. // This method gets called by the runtime. Use this method to add services to the container.s
  28. public void ConfigureServices(IServiceCollection services)
  29. {
  30. services.AddControllersWithViews();
  31. services.AddRouting(options =>
  32. {
  33. options.LowercaseUrls = true;
  34. });
  35. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  36. services.Configure<Setting>(Configuration.GetSection("Setting"));
  37. // 测试完成后必须注释掉
  38. // services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));//是否允许跨域
  39. // 测试完成后必须注释掉
  40. services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true).Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
  41. services.AddMvcCore().AddNewtonsoftJson();
  42. services.AddMvc(options =>
  43. {
  44. options.EnableEndpointRouting = false;
  45. options.Filters.Add(typeof(GlobalExceptions));
  46. });
  47. services.AddSession(options =>
  48. {
  49. // 设置 Session 过期时间
  50. options.IdleTimeout = TimeSpan.FromHours(1);
  51. options.Cookie.HttpOnly = true;
  52. });
  53. services.Configure<FormOptions>(x =>
  54. {
  55. x.MultipartBodyLengthLimit = 50 * 1024 * 1024;//不到300M
  56. });
  57. services.AddControllers(option =>
  58. {
  59. option.Filters.Add(new GlobalActionFilter());
  60. });
  61. //生成密钥
  62. var symmetricKeyAsBase64 = Configuration["Setting:JwtSecret"];
  63. var keyByteArray = Encoding.ASCII.GetBytes(symmetricKeyAsBase64);
  64. var signingKey = new SymmetricSecurityKey(keyByteArray);
  65. //认证参数
  66. services.AddAuthentication("Bearer").AddJwtBearer(o =>
  67. {
  68. o.TokenValidationParameters = new TokenValidationParameters
  69. {
  70. ValidateIssuerSigningKey = true,//是否验证签名,不验证的画可以篡改数据,不安全
  71. IssuerSigningKey = signingKey,//解密的密钥
  72. ValidateIssuer = true,//是否验证发行人,就是验证载荷中的Iss是否对应ValidIssuer参数
  73. // ValidIssuer = Configuration["Setting:JwtIss"],//发行人
  74. IssuerValidator = (m, n, z) =>
  75. {
  76. return n.Issuer;
  77. },
  78. ValidateAudience = true,//是否验证订阅人,就是验证载荷中的Aud是否对应ValidAudience参数
  79. // ValidAudience = Configuration["Setting:JwtAud"],//订阅人
  80. AudienceValidator = (m, n, z) =>
  81. {
  82. // if(n.Issuer.StartsWith("new_"))
  83. // {
  84. string check = RedisDbconn.Instance.Get<string>("utoken:" + n.Issuer);
  85. return m != null && m.FirstOrDefault().Equals(check);
  86. // }
  87. // else
  88. // {
  89. // string check = RedisDbconn.Instance.Get<string>("utoken:" + n.Issuer);
  90. // return m != null && m.FirstOrDefault().Equals(check);
  91. // }
  92. },
  93. ValidateLifetime = true,//是否验证过期时间,过期了就拒绝访问
  94. ClockSkew = TimeSpan.Zero,//这个是缓冲过期时间,也就是说,即使我们配置了过期时间,这里也要考虑进去,过期时间+缓冲,默认好像是7分钟,你可以直接设置为0
  95. RequireExpirationTime = true,
  96. };
  97. });
  98. //services.AddHttpContextAccessor();
  99. MySystemLib.SystemPublicFuction.appcheck = "success";
  100. }
  101. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  102. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  103. {
  104. if (env.IsDevelopment())
  105. {
  106. app.UseDeveloperExceptionPage();
  107. Library.ConfigurationManager.EnvironmentFlag = 1;
  108. }
  109. else
  110. {
  111. app.UseHsts();
  112. Library.ConfigurationManager.EnvironmentFlag = 2;
  113. }
  114. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  115. app.UseStaticFiles();
  116. // app.UseStaticFiles(new StaticFileOptions
  117. // {
  118. // FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/static"),
  119. // RequestPath = "/static"
  120. // });
  121. // app.UseStaticFiles(new StaticFileOptions
  122. // {
  123. // FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/" + Configuration["Setting:Database"]),
  124. // RequestPath = "/" + Configuration["Setting:Database"]
  125. // });
  126. app.UseStaticFiles(new StaticFileOptions
  127. {
  128. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  129. {
  130. { ".apk", "application/vnd.android.package-archive" }
  131. })
  132. });
  133. app.UseCors("cors");
  134. app.UseAuthentication();
  135. app.UseRouting();
  136. app.UseAuthorization();
  137. app.UseSession();
  138. app.UseEndpoints(endpoints =>
  139. {
  140. endpoints.MapControllerRoute(
  141. name: "default",
  142. pattern: "{controller=Home}/{action=Index}/{Id?}");
  143. });
  144. InitMain();
  145. InitStat();
  146. InitBs();
  147. }
  148. private void InitMain()
  149. {
  150. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  151. string connstr = Configuration["Setting:SqlConnStr"];
  152. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'QrCodePlateMainServer'", connstr);
  153. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  154. {
  155. Dictionary<string, string> Columns = new Dictionary<string, string>();
  156. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'QrCodePlateMainServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", connstr);
  157. foreach (System.Data.DataRow column in columncollection.Rows)
  158. {
  159. string datatype = column["DATA_TYPE"].ToString();
  160. if (datatype == "decimal")
  161. {
  162. datatype = "numeric";
  163. }
  164. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  165. }
  166. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  167. }
  168. AppConfig.Base.mainTables = tables;
  169. }
  170. private void InitStat()
  171. {
  172. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  173. string connstr = Configuration["Setting:StatSqlConnStr"];
  174. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'QrCodePlateStatServer'", connstr);
  175. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  176. {
  177. Dictionary<string, string> Columns = new Dictionary<string, string>();
  178. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'QrCodePlateStatServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", connstr);
  179. foreach (System.Data.DataRow column in columncollection.Rows)
  180. {
  181. string datatype = column["DATA_TYPE"].ToString();
  182. if (datatype == "decimal")
  183. {
  184. datatype = "numeric";
  185. }
  186. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  187. }
  188. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  189. }
  190. AppConfig.Base.statTables = tables;
  191. }
  192. private void InitBs()
  193. {
  194. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  195. string connstr = Configuration["Setting:BsSqlConnStr"];
  196. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'QrCodePlateBsServer'", connstr);
  197. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  198. {
  199. Dictionary<string, string> Columns = new Dictionary<string, string>();
  200. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'QrCodePlateBsServer' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", connstr);
  201. foreach (System.Data.DataRow column in columncollection.Rows)
  202. {
  203. string datatype = column["DATA_TYPE"].ToString();
  204. if (datatype == "decimal")
  205. {
  206. datatype = "numeric";
  207. }
  208. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  209. }
  210. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  211. }
  212. AppConfig.Base.bsTables = tables;
  213. }
  214. }
  215. }