I would like to have custom implementation of [Authorize] attribute in controlles.
This is what I did.
StartupClass in ConfigureServices
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
{
...
}
What I'm I missing? MyRequirement authorizationhader is never called. Thank you.
I believe you are missing this part:
services.AddSingleton<IAuthorizationHandler, MyRequirementHandler>();
Source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With