我想在控件中自定义实现[Authorize]属性。
这就是我所做的。
ConfigureServices中的StartupClass
services.AddAuthorization(options =>
{
options.AddPolicy("Authorize", policy =>
{
policy.AddRequirements(new MyRequirement());
});
});
MyRequirement
public class MyRequirement : AuthorizationHandler<MyRequirement>, IAuthorizationRequirement
{
protected override void Handle(AuthorizationContext context, MyRequirement requirement)
{
//some work
//if shloud be authorized
context.Succeed(requirement);
}
}
的TestController
[Authorize("Authorize")]
[Route("api/[controller]")]
public class TestController : Controller
{
...
}
我错过了什么?永远不会调用MyRequirement授权程序。 谢谢。
答案 0 :(得分:6)