Startup.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IdentityModel.Tokens.Jwt;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Text.Json;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. using HTEXLib.Builders;
  11. using HTEXLib.Translator;
  12. using Microsoft.AspNetCore.Authentication.JwtBearer;
  13. using Microsoft.AspNetCore.Builder;
  14. using Microsoft.AspNetCore.Hosting;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Http.Features;
  17. using Microsoft.AspNetCore.Mvc;
  18. using Microsoft.AspNetCore.SpaServices;
  19. using Microsoft.Extensions.Configuration;
  20. using Microsoft.Extensions.DependencyInjection;
  21. using Microsoft.Extensions.Hosting;
  22. using Microsoft.IdentityModel.Tokens;
  23. using TEAMModelOS.Models;
  24. using TEAMModelOS.SDK;
  25. using TEAMModelOS.SDK.Context.Attributes.Azure;
  26. using TEAMModelOS.SDK.Context.Configuration;
  27. using TEAMModelOS.SDK.DI;
  28. using TEAMModelOS.SDK.Models.Service;
  29. using VueCliMiddleware;
  30. namespace TEAMModelOS
  31. {
  32. public class Startup
  33. {
  34. public IWebHostEnvironment environment { get; set; }
  35. readonly string MyAllowSpecificOrigins = "_myAllowSpecificOrigins";
  36. //private IServiceCollection _services;
  37. public Startup(IConfiguration configuration, IWebHostEnvironment env)
  38. {
  39. Configuration = configuration;
  40. environment = env;
  41. BaseConfigModel.SetBaseConfig(Configuration, env.ContentRootPath, env.WebRootPath);
  42. }
  43. public IConfiguration Configuration { get; }
  44. // This method gets called by the runtime. Use this method to add services to the container.
  45. public void ConfigureServices(IServiceCollection services)
  46. {
  47. // true,默認情況下,聲明映射將以舊格式映射聲明名稱,以適應較早的SAML應用程序,RoleClaimType = 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role'
  48. // false,RoleClaimType = 'roles'
  49. JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
  50. services.AddAuthentication(options => options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme)
  51. .AddJwtBearer(options => //AzureADJwtBearer
  52. {
  53. //options.SaveToken = true; //驗證令牌由服務器生成才有效,不適用於服務重啟或分布式架構
  54. options.Authority = Configuration["Option:Authority"];
  55. options.Audience = Configuration["Option:Audience"];
  56. options.RequireHttpsMetadata = true;
  57. options.TokenValidationParameters = new TokenValidationParameters
  58. {
  59. RoleClaimType = "roles",
  60. ValidAudiences = new string[] { Configuration["Option:Audience"], $"api://{Configuration["Option:Audience"]}" }
  61. };
  62. options.Events = new JwtBearerEvents();
  63. //下列事件有需要紀錄則打開
  64. //options.Events.OnMessageReceived = async context => { await Task.FromResult(0); };
  65. //options.Events.OnForbidden = async context => { await Task.FromResult(0); };
  66. //options.Events.OnChallenge = async context => { await Task.FromResult(0); };
  67. //options.Events.OnAuthenticationFailed = async context => { await Task.FromResult(0); };
  68. options.Events.OnTokenValidated = async context =>
  69. {
  70. if (!context.Principal.Claims.Any(x => x.Type == "http://schemas.microsoft.com/identity/claims/scope") //ClaimConstants.Scope
  71. && !context.Principal.Claims.Any(y => y.Type == "roles")) //ClaimConstants.Roles //http://schemas.microsoft.com/ws/2008/06/identity/claims/role
  72. {
  73. //TODO 需處理額外授權非角色及範圍的訪問異常紀錄
  74. throw new UnauthorizedAccessException("Neither scope or roles claim was found in the bearer token.");
  75. }
  76. await Task.FromResult(0);
  77. };
  78. });
  79. //設定跨域請求
  80. services.AddCors(options =>
  81. {
  82. options.AddPolicy(MyAllowSpecificOrigins,
  83. builder =>
  84. {
  85. builder.WithOrigins("http://teammodelos-test.chinacloudsites.cn",
  86. "https://www.teammodel.cn", "https://localhost:5001",
  87. "http://localhost:5000", "http://localhost:64524",
  88. "https://localhost:44341", "https://localhost:8888", "http://localhost:8888")
  89. .AllowAnyHeader()
  90. .AllowAnyMethod();
  91. });
  92. });
  93. services.AddAzureStorage(Configuration.GetValue<string>("Azure:Storage:ConnectionString"));
  94. services.AddAzureRedis(Configuration.GetValue<string>("Azure:Redis:ConnectionString"));
  95. services.AddAzureCosmos(Configuration.GetValue<string>("Azure:Cosmos:ConnectionString"));
  96. services.AddAzureServiceBus(Configuration.GetValue<string>("Azure:ServiceBus:ConnectionString"));
  97. services.AddSnowflakeId(Convert.ToInt64(Configuration.GetValue<string>("Option:LocationNum")), 1);
  98. services.AddHttpClient();
  99. services.AddHttpClient<DingDing>();
  100. services.AddHttpClient<NotificationService>();
  101. services.AddMemoryCache();
  102. services.AddSpaStaticFiles(opt => opt.RootPath = "ClientApp/dist");
  103. services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = false; });
  104. //HttpContextAccessor,并用来访问HttpContext。(提供組件或非控制器服務存取HttpContext)
  105. services.AddHttpContextAccessor();
  106. services.Configure<Option>(options => Configuration.GetSection("Option").Bind(options));
  107. //注入word 標籤解析
  108. string path = $"{ environment.ContentRootPath}/JsonFile/Core";
  109. services.AddHtexTranslator(path);
  110. }
  111. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
  112. public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  113. {
  114. if (env.IsDevelopment())
  115. {
  116. app.UseDeveloperExceptionPage();
  117. }
  118. //TODO 目前不使用中間件全局攔截Exception,請在API中,明確處理200成功返回值或錯誤碼,Exception一率返回BadRequert 400,並選擇需要返回釘釘群組回報的API
  119. //app.UseMiddleware<HttpGlobalExceptionInvoke>();
  120. //以下需要按照順序載入中間件 如果应用调用 UseStaticFiles,请将 UseStaticFiles 置于 UseRouting之前。
  121. app.UseStaticFiles();
  122. //PRODUCTION uses webpack static files
  123. app.UseSpaStaticFiles();
  124. app.UseRouting();
  125. app.UseCors(MyAllowSpecificOrigins); //使用跨域設定
  126. app.UseHttpsRedirection(); //開發中暫時關掉
  127. //如果应用使用身份验证/授权功能(如 AuthorizePage 或 [Authorize]),请将对 UseAuthentication 和 UseAuthorization的
  128. //调用放在之后、UseRouting 和 UseCors,但在 UseEndpoints之前
  129. app.UseAuthentication();
  130. app.UseAuthorization();
  131. app.UseEndpoints(endpoints =>
  132. {
  133. endpoints.MapControllers();
  134. #if DEBUG
  135. endpoints.MapToVueCliProxy(
  136. "{*path}",
  137. new SpaOptions { SourcePath = "ClientApp" },
  138. npmScript: (System.Diagnostics.Debugger.IsAttached) ? "serve" : null,
  139. // regex: "Compiled successfully",
  140. forceKill: true
  141. );
  142. #else
  143. endpoints.MapFallbackToFile("index.html");
  144. #endif
  145. });
  146. }
  147. }
  148. }