'How to access user data in configuring ASP.NET Core authorization?

I'm trying to add conditional policy for authorization.

Here's my code:

services.AddAuthorization(options =>
{
    options.AddPolicy("RoleChecker", policy =>
    {
        if (user.IsSuperAdmin) // how can I access logged-in user here?
        {
            policy.RequireAssertion(i => true);
        }
        else
        {
            policy.RequireRole(Config.Role);
        }
    });
});

Basically I want to see if the logged in user is super admin, and in that case remove all checks. Otherwise, I want to inject a policy.

Is it possible?

How can I do that?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source