【发布时间】:2019-12-03 22:52:04
【问题描述】:
我已经成功实现了policy-based authorization in ASP.NET Core 2.2。我相信我理解AuthorizationHandlers can perform requirement checks on an OR-basis 的概念。
但除非我遗漏了什么,否则不能在单个处理程序中完成相同的 OR 基础评估吗?为什么不直接使用 if 语句来说明此需求是否具有此属性,请执行此操作;或者如果这些条件中的任何一个通过,则要求成功。甚至他们页面上带有BuildingEntryRequirement 的示例似乎也可以通过单个处理程序完成:
public class ExampleBuildingEntryHandler : AuthorizationHandler<BuildingEntryRequirement>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context,
BuildingEntryRequirement requirement)
{
if (context.User.HasClaim(c => c.Type == "TemporaryBadgeId" && c.Issuer == "https://microsoftsecurity") ||
context.User.HasClaim(c => c.Type == "BadgeId" && c.Issuer == "https://microsoftsecurity"))
{
// We'd also check the expiration date on the sticker.
context.Succeed(requirement);
}
//TODO: Use the following if targeting a version of
//.NET Framework older than 4.6:
// return Task.FromResult(0);
return Task.CompletedTask;
}
}
我是否缺少需要使用多个处理程序的场景?
【问题讨论】:
标签: c# asp.net-core-2.2 asp.net-authorization