public TestController(IEnumerable <IActionDescriptorChangeProvider> actionDescriptorChangeProviders, PluginFinder pluginFinder, IOptionsMonitor <RemConfig> optionsMonitor) { _actionDescriptorChangeProviders = actionDescriptorChangeProviders.ToList(); _pluginFinder = pluginFinder; _remConfig = optionsMonitor.CurrentValue; }
// This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { // 配置注入 _remConfig = _configuration.GetSection(RemConfig.Rem).Get <RemConfig>(); services.Configure <RemConfig>(_configuration.GetSection( RemConfig.Rem)); #region 择数据库类型 string dbType = _configuration["Rem:DbType"]; string connStr = _configuration.GetConnectionString("DefaultConnection"); switch (dbType.ToLower()) { case "sqlite": if (connStr.StartsWith("~")) { // 相对路径转绝对路径 string dir = Directory.GetCurrentDirectory(); string dbFilePath = Path.Combine(dir, connStr); connStr = dbFilePath; } services.AddDbContext <RemDbContext>(options => options.UseSqlite(connStr)); break; case "mysql": services.AddDbContext <RemDbContext>(options => options.UseMySQL(connStr)); break; case "sqlserver": services.AddDbContext <RemDbContext>(options => options.UseSqlServer(connStr)); break; default: if (connStr.StartsWith("~")) { // 相对路径转绝对路径 string dir = Directory.GetCurrentDirectory(); string dbFilePath = Path.Combine(dir, connStr); connStr = dbFilePath; } services.AddDbContext <RemDbContext>(options => options.UseSqlite(connStr)); break; } #endregion //services.AddControllers(); #region for UHub IdentityServer4 // accepts any access token issued by identity server services.AddAuthentication("Bearer") .AddJwtBearer("Bearer", options => { options.Authority = _configuration["Rem:Authority"]; options.TokenValidationParameters = new TokenValidationParameters { ValidateAudience = false, // 多长时间来验证以下 Token ClockSkew = TimeSpan.FromSeconds(5), // 我们要求 Token 需要有超时时间这个参数 RequireExpirationTime = true, }; options.RequireHttpsMetadata = false; }); #endregion #region 添加授权策略-所有标记 [WebApiAuthorize] 都需要权限检查 services.AddSingleton <IAuthorizationHandler, WebApiAuthorizationHandler>(); // adds an authorization policy to make sure the token is for scope 'webapi' services.AddAuthorization(options => { options.AddPolicy("WebApi", policy => { policy.RequireAuthenticatedUser(); // 1.需要 JWT scope 中包含 Remember.Core policy.RequireClaim("scope", "Remember.Core"); // 2.需要 检查是否拥有当前请求资源的权限 policy.Requirements.Add(new WebApiRequirement()); }); }); #endregion // MVC: Install 页面使用 Views services.AddControllersWithViews(); // 添加插件框架 services.AddPluginFramework(); // 开发环境下随便跨域 if (_webHostEnvironment.IsDevelopment()) { services.AddCors(m => m.AddPolicy("Development", a => a.AllowAnyOrigin().AllowAnyHeader())); } }