Startup.cs 12 KB

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