Search code examples
asp.net-coreauthorizationpolicy

How can I pass a parameter to an authorization handler used in a policy, through a controller


I've been searching for an answer to this, but I can't seem to find a straight answer. I have an Authorization Handler that is used in a Policy. The typical example is below:

services.AddAuthorization(options =>
    {
        options.AddPolicy("AtLeast21", policy =>
            policy.Requirements.Add(new MinimumAgeRequirement(21)));
    });

And then the policy is specified in the authorization attribute on the controller. What I want to do is specify the age requirement at the controller level. For Example:

[Authorize(Policy = "AtLeast21", 21)]

The idea would be that I could use the same Authorization Handler for multiple controller actions, or multiple policies.

Does anyone know how to do this?


Solution

  • You can customize authorization attributes and authorization policy providers to meet your needs.

    You can click on this link to view a detailed example I wrote earlier.