Search code examples
authorizationhandler.net-core-3.0claimspolicies

AuthorizationHandlerContext returns null User in custom Authorization Handler


I am trying to implement a custom authorization requirement, but it seems like context.User.Identity does not contain any data so the ClaimsPrincipal User.

  • I added service and policy like this

     services.AddAuthorization(config =>
                 {
                     config.AddPolicy("Test", policyBuilder =>
                     {
                         policyBuilder.UserRequireCustomClaim(0);
                     });
                 });
    
     services.AddScoped<IAuthorizationHandler, TestHandler>();
    

my requirement, builder and handler are also set. How should I fix this problem in ASP.NET Core 3.0?


Solution

  • I found my problem after a good rest and reviewing my code, i just extended my IdentityUser but didnt send it to base;

    public ApplicationUser() : base() { }
    

    so context couldnt be represented inside the handler. Problem solved...