123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- using System;
- using System.Collections.Generic;
- using System.IdentityModel.Tokens.Jwt;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Text.Json;
- using System.Threading;
- using System.Threading.Tasks;
- using Microsoft.AspNetCore.Authentication.JwtBearer;
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Http.Features;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.AspNetCore.SpaServices;
- using Microsoft.Azure.ServiceBus;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Hosting;
- using Microsoft.IdentityModel.Tokens;
- using Scrutor;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Context.Attributes.Azure;
- using TEAMModelOS.SDK.Context.Configuration;
- using TEAMModelOS.SDK.Context.Filter;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Module.AzureServiceBus;
- using TEAMModelOS.Service.Services.Learn.Implements;
- using TEAMModelOS.Service.Services.Learn.Interfaces;
- using VueCliMiddleware;
- namespace TEAMModelOS
- {
- public class Startup
- {
- //private IServiceCollection _services;
- public Startup(IConfiguration configuration, IWebHostEnvironment env)
- {
- Configuration = configuration;
- BaseConfigModel.SetBaseConfig(Configuration, env.ContentRootPath, env.WebRootPath);
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- // true,默認情況下,聲明映射將以舊格式映射聲明名稱,以適應較早的SAML應用程序,RoleClaimType = 'http://schemas.microsoft.com/ws/2008/06/identity/claims/role'
- // false,RoleClaimType = 'roles'
- JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
- services.AddAuthentication(options => options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme)
- .AddJwtBearer(options => //AzureADJwtBearer
- {
- //options.SaveToken = true; //驗證令牌由服務器生成才有效,不適用於服務重啟或分布式架構
- options.Authority = Configuration["Option:Authority"];
- options.Audience = Configuration["Option:Audience"];
- options.RequireHttpsMetadata = true;
- options.TokenValidationParameters = new TokenValidationParameters
- {
- RoleClaimType = "roles",
- ValidAudiences = new string[] { Configuration["Option:Audience"], $"api://{Configuration["Option:Audience"]}" }
- };
- options.Events = new JwtBearerEvents();
- //下列事件有需要紀錄則打開
- //options.Events.OnMessageReceived = async context => { await Task.FromResult(0); };
- //options.Events.OnForbidden = async context => { await Task.FromResult(0); };
- //options.Events.OnChallenge = async context => { await Task.FromResult(0); };
- //options.Events.OnAuthenticationFailed = async context => { await Task.FromResult(0); };
- options.Events.OnTokenValidated = async context =>
- {
- if (!context.Principal.Claims.Any(x => x.Type == "http://schemas.microsoft.com/identity/claims/scope") //ClaimConstants.Scope
- && !context.Principal.Claims.Any(y => y.Type == "roles")) //ClaimConstants.Roles //http://schemas.microsoft.com/ws/2008/06/identity/claims/role
- {
- //TODO 需處理額外授權非角色及範圍的訪問異常紀錄
- throw new UnauthorizedAccessException("Neither scope or roles claim was found in the bearer token.");
- }
- await Task.FromResult(0);
- };
- });
-
- services.AddAzureStorage(Configuration.GetValue<string>("Azure:Starage:ConnectionString"));
- services.AddAzureRedis( Configuration.GetValue<string>("Azure:Redis:ConnectionString"));
- services.AddAzureCosmos(Configuration.GetValue<string>("Azure:Cosmos:ConnectionString"));
- services.AddSnowflakeId(Convert.ToInt64(Configuration.GetValue<string>("Option:LocationNum")), 1);
- services.AddHttpClient();
- services.AddHttpClient<DingDing>();
- services.AddMemoryCache();
- services.AddControllers().AddJsonOptions(options => { options.JsonSerializerOptions.IgnoreNullValues = false; });
- services.Configure<Option>(options => Configuration.GetSection("Option").Bind(options));
- //Table配置
- // services.AddScoped<IAzureTableDBRepository, AzureTableDBRepository>();
- //使用Blob配置
- //services.AddAzureBlobStorage().AddConnection(Configuration.GetSection("Azure:Blob").Get<AzureBlobOptions>());
- //使用AzureServiceBus
- services.AddServiceBus().AddServiceBusOptions(Configuration.GetSection("Azure:ServiceBus").Get<AzureServiceBusOptions>());
- services.AddSingleton<IServiceBusService, ServiceBusService>();
- //HttpContextAccessor,并用来访问HttpContext。
- services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
- //services.AddSingleton<IServiceBusReviceService, ServiceBusReviceService>();
- //注入CSRedis
- var csredis = new CSRedis.CSRedisClient(Configuration.GetSection("Azure:Redis:ConnectionString").Get<string>());
- RedisHelper.Initialization(csredis);
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env )
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- //app.UseMiddleware<HttpGlobalExceptionInvoke>();
- //以下需要按照順序載入中間件 如果应用调用 UseStaticFiles,请将 UseStaticFiles 置于 UseRouting之前。
- app.UseStaticFiles();
- //app.UseSpaStaticFiles(); //使用中間件不開
- app.UseRouting();
- //app.UseCors(MyAllowSpecificOrigins); //使用跨域設定
- //app.UseHttpsRedirection(); //開發中暫時關掉
- //如果应用使用身份验证/授权功能(如 AuthorizePage 或 [Authorize]),请将对 UseAuthentication 和 UseAuthorization的
- //调用放在之后、UseRouting 和 UseCors,但在 UseEndpoints之前
- app.UseAuthentication();
- app.UseAuthorization();
- // app.UseJsonRpc();
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllers();
- #if DEBUG
- endpoints.MapToVueCliProxy(
- "{*path}",
- new SpaOptions { SourcePath = "ClientApp", },
- npmScript: (System.Diagnostics.Debugger.IsAttached) ? "serve" : null
- );
- #else
- endpoints.MapFallbackToFile("index.html");
- #endif
- //endpoints.MapGet("/", async context =>
- //{
- // await context.Response.WriteAsync("Hello World!");
- //});
- });
- }
- }
- }
|