我需要根据本地 Active Directory 中的一些信息实现自定义授权。在做了一些研究之后,我认为最好的方法是编写一个自定义身份验证过滤器并将 AD 中的信息添加到声明列表中。
因此,在 IIS 使用 Windows 身份验证对用户进行身份验证后,我计划阅读一些信息并将其放入声明列表中:
public async Task AuthenticateAsync(HttpAuthenticationContext context, CancellationToken cancellationToken)
{
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, userPrincipal.Name));
claims.Add(new Claim(ClaimTypes.Role, "client"));
claims.Add(new Claim("Accounts", "[List of accounts from AD]"));
var identity = new ClaimsIdentity(claims);
var principal = new ClaimsPrincipal(new[] { identity });
context.Principal = principal;
Thread.CurrentPrincipal = context.Principal;
}
我相信这种方法将允许我从任何控制器访问帐户列表。但是,我无法IAuthenticationFilter
使用以下方法将我的实现添加到全局过滤器列表中。
builder.Services.AddControllers(config =>
{
config.Filters.Add(new ApiAuthenticationFilter())
});
这个方法需要IFilterMetaData
接口,而我已经实现了IAuthenticationFilter
. 在以前的 Web API 版本中,我们可以通过方法访问,但HttpConfiguration
在Application_Start()
ASP.NET Core 6 Web API 中,我无法找到将过滤器添加到HttpConfiguration
.
您能否告诉我这是否是正确的方法,或者我应该尝试实现IActionFilter
接口?或者完全不同的方法。
谢谢!