I put this here for reference because I have been thinking about it for too long ...
I met the special requirement and the handler (empty for testing):
using Microsoft.AspNetCore.Authorization; using System.Threading.Tasks; public class TestHandler : AuthorizationHandler<TestRequirement>, IAuthorizationRequirement { protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, TestRequirement requirement) { context.Succeed(requirement); return Task.CompletedTask; } } public class TestRequirement : IAuthorizationRequirement { }
Register it in the Startup.cs ConfigureServices() section:
services.AddAuthorization(options => { options.AddPolicy("Test", policy => policy.Requirements.Add(new TestRequirement()));
Added it to my controller method:
[HttpGet] [Authorize(Policy = "Test")] public IActionResult Index() { Return View(); }
But it turned out a 403 error (not 401) with each request to the controller method!
Turns out I didn't register TestHandler with the ConfigureServices() (Injection of Dependency) Startup.cs in Startup.cs .
services.AddSingleton<IAuthorizationHandler, TestHandler>();
I hope this saves someone from hitting his head on the table .: |
source share