|
@@ -18,6 +18,7 @@ using Microsoft.Azure.ServiceBus;
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using Microsoft.Extensions.Hosting;
|
|
|
+using Microsoft.IdentityModel.Tokens;
|
|
|
using Scrutor;
|
|
|
using TEAMModelOS.SDK;
|
|
|
using TEAMModelOS.SDK.Context.Attributes.Azure;
|
|
@@ -30,24 +31,16 @@ using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
|
|
|
using TEAMModelOS.SDK.Module.AzureServiceBus;
|
|
|
using TEAMModelOS.SDK.Module.AzureTable.Implements;
|
|
|
using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
|
|
|
-using TEAMModelOS.Service.Exam.Implements;
|
|
|
-using TEAMModelOS.Service.Exam.Interfaces;
|
|
|
using TEAMModelOS.Service.Services.ChangeFeed;
|
|
|
-using TEAMModelOS.Service.Services.Exam.Implements;
|
|
|
-using TEAMModelOS.Service.Services.Exam.Interfaces;
|
|
|
-using TEAMModelOS.Service.Services.Implement;
|
|
|
-using TEAMModelOS.Service.Services.Interface;
|
|
|
using TEAMModelOS.Service.Services.Learn.Implements;
|
|
|
using TEAMModelOS.Service.Services.Learn.Interfaces;
|
|
|
-using TEAMModelOS.Service.Services.PowerPoint.Implement;
|
|
|
-using TEAMModelOS.Service.Services.PowerPoint.Interface;
|
|
|
using VueCliMiddleware;
|
|
|
|
|
|
namespace TEAMModelOS
|
|
|
{
|
|
|
public class Startup
|
|
|
{
|
|
|
- private IServiceCollection _services;
|
|
|
+ //private IServiceCollection _services;
|
|
|
public Startup(IConfiguration configuration, IWebHostEnvironment env)
|
|
|
{
|
|
|
Configuration = configuration;
|
|
@@ -55,50 +48,44 @@ namespace TEAMModelOS
|
|
|
}
|
|
|
|
|
|
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'
|
|
|
- /*
|
|
|
+ // false,RoleClaimType = 'roles'
|
|
|
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
|
|
|
- //這個設定ValidAudiences等專案準備要布署時,會在Azure CN 設置AD金鑰,晚一點處理沒關係,會搭配訪問其他雲服務時的Client金鑰獲取及刷新代碼,之後再提供
|
|
|
- var tokenvalidationparameters = new TokenValidationParameters
|
|
|
- {
|
|
|
- RoleClaimType = "roles",
|
|
|
- ValidAudiences = new string[] { "8768b06f-c5c5-4b0c-abfb-d7ded354626d", "api://8768b06f-c5c5-4b0c-abfb-d7ded354626d" },
|
|
|
- };
|
|
|
- services.AddAuthentication(options =>
|
|
|
- {
|
|
|
- options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
|
|
|
- }).AddJwtBearer(options => //AzureADJwtBearer
|
|
|
- {
|
|
|
- //options.SaveToken = true; //驗證令牌由服務器生成才有效,不適用於服務重啟或分布式架構
|
|
|
- options.Authority = "https://login.microsoftonline.com/73a2bcc5-fe99-4566-aa8a-07e7bb287df1/v2.0";
|
|
|
- options.RequireHttpsMetadata = true;
|
|
|
- options.Audience = "8768b06f-c5c5-4b0c-abfb-d7ded354626d";
|
|
|
- options.TokenValidationParameters = tokenvalidationparameters;
|
|
|
- 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 =>
|
|
|
+
|
|
|
+ services.AddAuthentication(options => options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme)
|
|
|
+ .AddJwtBearer(options => //AzureADJwtBearer
|
|
|
{
|
|
|
- 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
|
|
|
+ //options.SaveToken = true; //驗證令牌由服務器生成才有效,不適用於服務重啟或分布式架構
|
|
|
+ options.Authority = Configuration["Option:Authority"];
|
|
|
+ options.Audience = Configuration["Option:Audience"];
|
|
|
+ options.RequireHttpsMetadata = true;
|
|
|
+ options.TokenValidationParameters = new TokenValidationParameters
|
|
|
{
|
|
|
- //TODO 需處理額外授權非角色及範圍的訪問異常紀錄
|
|
|
- throw new UnauthorizedAccessException("Neither scope or roles claim was found in the bearer token.");
|
|
|
- }
|
|
|
- await Task.FromResult(0);
|
|
|
- };
|
|
|
- });
|
|
|
- */
|
|
|
+ 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.AddCors(options =>
|
|
|
//{
|
|
@@ -123,7 +110,7 @@ namespace TEAMModelOS
|
|
|
x.MultipartBodyLengthLimit = long.MaxValue; // In case of multipart
|
|
|
x.MultipartHeadersLengthLimit = int.MaxValue;
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
// Table配置
|
|
|
services.AddScoped<IAzureTableDBRepository, AzureTableDBRepository>();
|
|
|
//使用Blob配置
|
|
@@ -138,39 +125,36 @@ namespace TEAMModelOS
|
|
|
services.AddSingleton<IServiceBusReviceService, ServiceBusReviceService>();
|
|
|
//注入CSRedis
|
|
|
var csredis = new CSRedis.CSRedisClient(Configuration.GetSection("Azure:Redis:ConnectionString").Get<string>());
|
|
|
- // CSRedis.CSRedisClient.Serialize = obj =>System.Text.Json.JsonSerializer.Serialize(obj);
|
|
|
- // CSRedis.CSRedisClient.Deserialize = (json, type) => System.Text.Json.JsonSerializer.Deserialize(json, type);
|
|
|
+ // CSRedis.CSRedisClient.Serialize = obj =>System.Text.Json.JsonSerializer.Serialize(obj);
|
|
|
+ // CSRedis.CSRedisClient.Deserialize = (json, type) => System.Text.Json.JsonSerializer.Deserialize(json, type);
|
|
|
RedisHelper.Initialization(csredis);
|
|
|
- // RedisHelper.Instance.CurrentSerialize = obj => System.Text.Json.JsonSerializer.Serialize(obj);
|
|
|
+ // RedisHelper.Instance.CurrentSerialize = obj => System.Text.Json.JsonSerializer.Serialize(obj);
|
|
|
//services.AddSingleton<PowerPointService, PowerPointService>();
|
|
|
|
|
|
|
|
|
//全局扫描基于IBusinessService接口的实现类
|
|
|
- //services.Scan(scan => scan.FromApplicationDependencies()
|
|
|
- // .AddClasses(classes => classes.AssignableTo<IBusinessService>())
|
|
|
- // .AsImplementedInterfaces()
|
|
|
- // .WithScopedLifetime());
|
|
|
- //services.Scan(scan => scan.FromApplicationDependencies()
|
|
|
- // .AddClasses(classes => classes.AssignableTo<IChangeService>()).UsingRegistrationStrategy(RegistrationStrategy.Skip)
|
|
|
- // .AsImplementedInterfaces()
|
|
|
- // .WithSingletonLifetime());
|
|
|
-
|
|
|
-
|
|
|
- _services = services;
|
|
|
+ services.Scan(scan => scan.FromApplicationDependencies()
|
|
|
+ .AddClasses(classes => classes.AssignableTo<IBusinessService>())
|
|
|
+ .AsImplementedInterfaces()
|
|
|
+ .WithScopedLifetime());
|
|
|
+ services.Scan(scan => scan.FromApplicationDependencies()
|
|
|
+ .AddClasses(classes => classes.AssignableTo<IChangeService>()).UsingRegistrationStrategy(RegistrationStrategy.Skip)
|
|
|
+ .AsImplementedInterfaces()
|
|
|
+ .WithSingletonLifetime());
|
|
|
+ // _services = services;
|
|
|
}
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
|
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IAzureCosmosDBV3Repository cosmosDBV3Repository,
|
|
|
- IChangeFeedInvoke changeFeedInvoke,IAzureServiceBusService azureServiceBusService , IServiceBusReviceService serviceBusReviceService)
|
|
|
+ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, IAzureCosmosDBV3Repository cosmosDBV3Repository )
|
|
|
{
|
|
|
if (env.IsDevelopment())
|
|
|
{
|
|
|
app.UseDeveloperExceptionPage();
|
|
|
}
|
|
|
- CosmosDict dict= cosmosDBV3Repository.InitializeDatabase().Result;
|
|
|
- changeFeedInvoke.MonitorChangeFeed(dict, _services);
|
|
|
- azureServiceBusService.init();
|
|
|
- serviceBusReviceService.ReciveMessageAsync();
|
|
|
+ CosmosDict dict = cosmosDBV3Repository.InitializeDatabase().Result;
|
|
|
+ //changeFeedInvoke.MonitorChangeFeed(dict, _services);
|
|
|
+ //azureServiceBusService.init();
|
|
|
+ //serviceBusReviceService.ReciveMessageAsync();
|
|
|
app.UseMiddleware<HttpGlobalExceptionInvoke>();
|
|
|
//以下需要按照順序載入中間件 如果应用调用 UseStaticFiles,请将 UseStaticFiles 置于 UseRouting之前。
|
|
|
app.UseStaticFiles();
|