通过配置添加 AuthorizationHandler

Adding an AuthorizationHandler via config

在 WCF 中,您可以在服务行为中使用 serviceAuthorization 节点通过 web.config 添加授权策略。有没有办法通过配置在 .NET Core WebAPI 中包含 AuthorizationHandler

明确地说,我试图用 web.config 中的内容替换 Startup.cs 中的这一行:

services.AddSingleton<IAuthorizationHandler, MyAuthorizationHandler>();

web.config 仅用于 IIS 特定配置。由于 .net-core 的跨平台特性,他们放弃了与 Web 配置的耦合以进行应用程序配置。

A web.config file is required when hosting the app in IIS or IIS Express. Settings in web.config enable the ASP.NET Core Module to launch the app and configure other IIS settings and modules.

引用Configure an ASP.NET Core App: The web.config file

Startup 是您进入应用程序的入口点,您可以在 json 文件中进行一些设置,并让您的代码 add/update 配置基于那。

我的想法是每次你想添加一些东西时都不必重新编译,因为配置选项允许你 Reload configuration data with IOptionsSnapshot
需要 ASP.NET Core 1.1 或更高版本。

IOptionsSnapshot supports reloading options with minimal processing overhead. In ASP.NET Core 1.1, IOptionsSnapshot is a snapshot of IOptionsMonitor<TOptions> and updates automatically whenever the monitor triggers changes based on the data source changing. In ASP.NET Core 2.0 and later, options are computed once per request when accessed and cached for the lifetime of the request.

您的授权处理程序将取决于选项并根据提供的配置执行其功能。