Startup.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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.Mvc.Razor;
  18. using Microsoft.AspNetCore.Server.Kestrel.Core;
  19. namespace MySystem
  20. {
  21. public class Startup
  22. {
  23. public Startup(IConfiguration configuration)
  24. {
  25. Configuration = configuration;
  26. }
  27. public IConfiguration Configuration { get; }
  28. // This method gets called by the runtime. Use this method to add services to the container.
  29. public void ConfigureServices(IServiceCollection services)
  30. {
  31. services.AddControllersWithViews();
  32. services.AddRouting(options =>
  33. {
  34. options.LowercaseUrls = true;
  35. });
  36. services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
  37. // services.AddCors(option => option.AddPolicy("cors", policy => policy.AllowAnyHeader().AllowAnyMethod().AllowCredentials().SetIsOriginAllowed(_ => true)));
  38. services.Configure<KestrelServerOptions>(x => x.AllowSynchronousIO = true).Configure<IISServerOptions>(x => x.AllowSynchronousIO = true);
  39. services.AddMvc(options =>
  40. {
  41. options.EnableEndpointRouting = false;
  42. options.Filters.Add(typeof(GlobalExceptionsFilter));
  43. });
  44. //配置模版视图路径
  45. services.Configure<RazorViewEngineOptions>(options =>
  46. {
  47. options.ViewLocationExpanders.Add(new TemplateViewLocationExpander());
  48. });
  49. services.AddSession(options =>
  50. {
  51. // 设置 Session 过期时间
  52. options.IdleTimeout = TimeSpan.FromHours(1);
  53. options.Cookie.HttpOnly = true;
  54. });
  55. services.Configure<FormOptions>(x =>
  56. {
  57. x.MultipartBodyLengthLimit = 50 * 1024 * 1024;//不到300M
  58. });
  59. //生成密钥
  60. var symmetricKeyAsBase64 = Configuration["Setting:JwtSecret"];
  61. var keyByteArray = Encoding.ASCII.GetBytes(symmetricKeyAsBase64);
  62. var signingKey = new SymmetricSecurityKey(keyByteArray);
  63. //认证参数
  64. services.AddAuthentication("Bearer").AddJwtBearer(o =>
  65. {
  66. o.TokenValidationParameters = new TokenValidationParameters
  67. {
  68. ValidateIssuerSigningKey = true,//是否验证签名,不验证的画可以篡改数据,不安全
  69. IssuerSigningKey = signingKey,//解密的密钥
  70. ValidateIssuer = true,//是否验证发行人,就是验证载荷中的Iss是否对应ValidIssuer参数
  71. // ValidIssuer = Configuration["Setting:JwtIss"],//发行人
  72. IssuerValidator = (m, n, z) =>
  73. {
  74. return n.Issuer;
  75. },
  76. ValidateAudience = true,//是否验证订阅人,就是验证载荷中的Aud是否对应ValidAudience参数
  77. // ValidAudience = Configuration["Setting:JwtAud"],//订阅人
  78. AudienceValidator = (m, n, z) =>
  79. {
  80. string check = RedisDbconn.Instance.Get<string>("utoken:" + n.Issuer);
  81. return m != null && m.FirstOrDefault().Equals(check);
  82. },
  83. ValidateLifetime = true,//是否验证过期时间,过期了就拒绝访问
  84. ClockSkew = TimeSpan.Zero,//这个是缓冲过期时间,也就是说,即使我们配置了过期时间,这里也要考虑进去,过期时间+缓冲,默认好像是7分钟,你可以直接设置为0
  85. RequireExpirationTime = true,
  86. };
  87. });
  88. MySystemLib.SystemPublicFuction.appcheck = "success";
  89. }
  90. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  91. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  92. {
  93. if (env.IsDevelopment())
  94. {
  95. app.UseDeveloperExceptionPage();
  96. Library.ConfigurationManager.EnvironmentFlag = 1;
  97. }
  98. else
  99. {
  100. app.UseExceptionHandler("/Home/Error");
  101. app.UseHsts();
  102. Library.ConfigurationManager.EnvironmentFlag = 2;
  103. }
  104. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  105. app.UseStaticFiles();
  106. app.UseStaticFiles(new StaticFileOptions
  107. {
  108. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  109. {
  110. { ".apk", "application/vnd.android.package-archive" }
  111. })
  112. });
  113. app.UseCors("cors");
  114. app.UseAuthentication();
  115. app.UseRouting();
  116. app.UseAuthorization();
  117. app.UseSession();
  118. app.UseEndpoints(endpoints =>
  119. {
  120. endpoints.MapControllerRoute(
  121. name: "default",
  122. pattern: "{controller=Home}/{action=Index}/{Id?}");
  123. });
  124. initKxsMainServer();//客小爽主库
  125. initMainServer();
  126. initMain1Server();
  127. InitStat();
  128. InitBs();
  129. }
  130. //初始化数据结构
  131. private void initKxsMainServer()
  132. {
  133. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  134. string connstr = Configuration["Setting:KxsSqlConnStr"];
  135. string dbName = "KxsMainServer";
  136. // if(Library.ConfigurationManager.EnvironmentFlag == 2)
  137. // {
  138. // dbName = "KxsProfitServer";
  139. // }
  140. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = '" + dbName + "'", connstr);
  141. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  142. {
  143. Dictionary<string, string> Columns = new Dictionary<string, string>();
  144. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = '" + dbName + "' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", connstr);
  145. foreach (System.Data.DataRow column in columncollection.Rows)
  146. {
  147. string datatype = column["DATA_TYPE"].ToString();
  148. if (datatype == "decimal")
  149. {
  150. datatype = "numeric";
  151. }
  152. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  153. }
  154. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  155. }
  156. AppConfig.Base.kxsMainTables = tables;
  157. }
  158. private void initMainServer()
  159. {
  160. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  161. string connstr = Configuration["Setting:SqlConnStr"];
  162. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'QrCodePlateMainServer2'", connstr);
  163. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  164. {
  165. Dictionary<string, string> Columns = new Dictionary<string, string>();
  166. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'QrCodePlateMainServer2' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", connstr);
  167. foreach (System.Data.DataRow column in columncollection.Rows)
  168. {
  169. string datatype = column["DATA_TYPE"].ToString();
  170. if (datatype == "decimal")
  171. {
  172. datatype = "numeric";
  173. }
  174. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  175. }
  176. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  177. }
  178. AppConfig.Base.mainTables = tables;
  179. }
  180. private void initMain1Server()
  181. {
  182. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  183. string connstr = Configuration["Setting:ZsSqlConnStr"];
  184. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'QrCodePlateMainServer'", connstr);
  185. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  186. {
  187. Dictionary<string, string> Columns = new Dictionary<string, string>();
  188. 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);
  189. foreach (System.Data.DataRow column in columncollection.Rows)
  190. {
  191. string datatype = column["DATA_TYPE"].ToString();
  192. if (datatype == "decimal")
  193. {
  194. datatype = "numeric";
  195. }
  196. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  197. }
  198. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  199. }
  200. AppConfig.Base.main1Tables = tables;
  201. }
  202. private void InitStat()
  203. {
  204. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  205. string connstr = Configuration["Setting:StatSqlConnStr"];
  206. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'QrCodePlateStatServer2'", connstr);
  207. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  208. {
  209. Dictionary<string, string> Columns = new Dictionary<string, string>();
  210. System.Data.DataTable columncollection = Library.CustomerSqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'QrCodePlateStatServer2' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'", connstr);
  211. foreach (System.Data.DataRow column in columncollection.Rows)
  212. {
  213. string datatype = column["DATA_TYPE"].ToString();
  214. if (datatype == "decimal")
  215. {
  216. datatype = "numeric";
  217. }
  218. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  219. }
  220. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  221. }
  222. AppConfig.Base.statTables = tables;
  223. }
  224. private void InitBs()
  225. {
  226. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  227. string connstr = Configuration["Setting:BsSqlConnStr"];
  228. System.Data.DataTable tablecollection = Library.CustomerSqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'QrCodePlateBsServer'", connstr);
  229. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  230. {
  231. Dictionary<string, string> Columns = new Dictionary<string, string>();
  232. 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);
  233. foreach (System.Data.DataRow column in columncollection.Rows)
  234. {
  235. string datatype = column["DATA_TYPE"].ToString();
  236. if (datatype == "decimal")
  237. {
  238. datatype = "numeric";
  239. }
  240. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  241. }
  242. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  243. }
  244. AppConfig.Base.bsTables = tables;
  245. }
  246. }
  247. }