Startup.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.AddMvc(options =>
  42. {
  43. options.EnableEndpointRouting = false;
  44. options.Filters.Add(typeof(GlobalExceptions));
  45. });
  46. services.AddSession(options =>
  47. {
  48. // 设置 Session 过期时间
  49. options.IdleTimeout = TimeSpan.FromHours(1);
  50. options.Cookie.HttpOnly = true;
  51. });
  52. services.Configure<FormOptions>(x =>
  53. {
  54. x.MultipartBodyLengthLimit = 50 * 1024 * 1024;//不到300M
  55. });
  56. //生成密钥
  57. var symmetricKeyAsBase64 = Configuration["Setting:JwtSecret"];
  58. var keyByteArray = Encoding.ASCII.GetBytes(symmetricKeyAsBase64);
  59. var signingKey = new SymmetricSecurityKey(keyByteArray);
  60. //认证参数
  61. services.AddAuthentication("Bearer").AddJwtBearer(o =>
  62. {
  63. o.TokenValidationParameters = new TokenValidationParameters
  64. {
  65. ValidateIssuerSigningKey = true,//是否验证签名,不验证的画可以篡改数据,不安全
  66. IssuerSigningKey = signingKey,//解密的密钥
  67. ValidateIssuer = true,//是否验证发行人,就是验证载荷中的Iss是否对应ValidIssuer参数
  68. // ValidIssuer = Configuration["Setting:JwtIss"],//发行人
  69. IssuerValidator = (m, n, z) =>
  70. {
  71. return n.Issuer;
  72. },
  73. ValidateAudience = true,//是否验证订阅人,就是验证载荷中的Aud是否对应ValidAudience参数
  74. // ValidAudience = Configuration["Setting:JwtAud"],//订阅人
  75. AudienceValidator = (m, n, z) =>
  76. {
  77. string check = RedisDbconn.Instance.Get<string>("utoken:" + n.Issuer);
  78. return m != null && m.FirstOrDefault().Equals(check);
  79. },
  80. ValidateLifetime = true,//是否验证过期时间,过期了就拒绝访问
  81. ClockSkew = TimeSpan.Zero,//这个是缓冲过期时间,也就是说,即使我们配置了过期时间,这里也要考虑进去,过期时间+缓冲,默认好像是7分钟,你可以直接设置为0
  82. RequireExpirationTime = true,
  83. };
  84. });
  85. //services.AddHttpContextAccessor();
  86. // 必须打开
  87. // services.AddHostedService<SycnStartService>();
  88. // 必须打开
  89. MySystemLib.SystemPublicFuction.appcheck = "success";
  90. }
  91. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  92. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  93. {
  94. if (env.IsDevelopment())
  95. {
  96. app.UseDeveloperExceptionPage();
  97. }
  98. else
  99. {
  100. app.UseHsts();
  101. }
  102. Library.function.WritePage("/", "WebRootPath.txt", env.WebRootPath);
  103. app.UseStaticFiles();
  104. // app.UseStaticFiles(new StaticFileOptions
  105. // {
  106. // FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/static"),
  107. // RequestPath = "/static"
  108. // });
  109. // app.UseStaticFiles(new StaticFileOptions
  110. // {
  111. // FileProvider = new PhysicalFileProvider(AppContext.BaseDirectory + "/" + Configuration["Setting:Database"]),
  112. // RequestPath = "/" + Configuration["Setting:Database"]
  113. // });
  114. app.UseStaticFiles(new StaticFileOptions
  115. {
  116. ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string>
  117. {
  118. { ".apk", "application/vnd.android.package-archive" }
  119. })
  120. });
  121. app.UseCors("cors");
  122. app.UseAuthentication();
  123. app.UseRouting();
  124. app.UseAuthorization();
  125. app.UseSession();
  126. app.UseEndpoints(endpoints =>
  127. {
  128. endpoints.MapControllerRoute(
  129. name: "default",
  130. pattern: "{controller=Home}/{action=Index}/{Id?}");
  131. });
  132. initMainServer();
  133. }
  134. private void initMainServer()
  135. {
  136. Dictionary<string, Dictionary<string, string>> tables = new Dictionary<string, Dictionary<string, string>>();
  137. Library.OtherMySqlConn.connstr = Configuration["Setting:SqlConnStr"];
  138. System.Data.DataTable tablecollection = Library.OtherMySqlConn.dtable("select DISTINCT TABLE_NAME from information_schema.columns where table_schema = 'MiniProgram'");
  139. foreach (System.Data.DataRow subtable in tablecollection.Rows)
  140. {
  141. Dictionary<string, string> Columns = new Dictionary<string, string>();
  142. System.Data.DataTable columncollection = Library.OtherMySqlConn.dtable("select COLUMN_NAME,DATA_TYPE from information_schema.columns where table_schema = 'MiniProgram' and TABLE_NAME='" + subtable["TABLE_NAME"].ToString() + "'");
  143. foreach (System.Data.DataRow column in columncollection.Rows)
  144. {
  145. string datatype = column["DATA_TYPE"].ToString();
  146. if (datatype == "decimal")
  147. {
  148. datatype = "numeric";
  149. }
  150. Columns.Add(column["COLUMN_NAME"].ToString(), datatype);
  151. }
  152. tables.Add(subtable["TABLE_NAME"].ToString(), Columns);
  153. }
  154. Library.OtherMySqlConn.connstr = "";
  155. PublicFunction.MainTables = tables;
  156. }
  157. }
  158. }