An authorization handler contains the logic for deciding whether an authorization requirement is satisfied. In ASP.NET Core, a policy brings requirements together; the requirements describe what must be satisfied, while handlers determine how to satisfy them. section 24.4.2 section 24.7
For a custom handler, derive from AuthorizationHandler<T>, where T is the requirement type, and override HandleRequirementAsync. The method receives the requirement and an AuthorizationHandlerContext containing the current user. Handlers can use dependency injection, while requirements are lightweight objects created directly. section 24.4.2
Listing 24.8 FrequentFlyerHandler for AllowedInLoungeRequirement
public class FrequentFlyerHandler :
AuthorizationHandler<AllowedInLoungeRequirement> #1
{
protected override Task HandleRequirementAsync( #2
AuthorizationHandlerContext context, #3
AllowedInLoungeRequirement requirement) #4
{
if(context.User.HasClaim("FrequentFlyerClass", "Gold")) #5
{
context.Succeed(requirement); #6
}
return Task.CompletedTask; #7
}
}
#1 The handler implements AuthorizationHandler<T>.
#2 You must override the abstract HandleRequirementAsync method.
#3 The context contains details such as the ClaimsPrincipal user object.
#4 The requirement instance to handle
#5 Checks whether the user has the Frequent-FlyerClass claim with the Gold value
#6 If the user had the necessary claim, marks the requirement as satisfied by calling Succeed
#7 If the requirement wasn’t satisfied, does nothing
This handler checks the user's FrequentFlyerClass claim and marks the lounge requirement as satisfied when its value is Gold. section 24.4.2
A handler has three possible responses:
- Call context.Succeed(requirement) when its check satisfies the requirement. section 24.7
- Do nothing when it cannot satisfy the requirement but another handler might. section 24.7
- Call context.Fail() only when authorization must fail even if another handler succeeds. section 24.7
Multiple ways to satisfy a requirement
A lounge policy requires a minimum age and permission to enter the lounge. Separate handlers can satisfy the lounge requirement for a Gold frequent flyer or an airline employee. A ban handler calls Fail() for a banned user, overriding any success. Every policy requirement must be satisfied, and no handler may explicitly fail authorization. section 24.4 section 24.4.2 section 24.7
Register each handler as an IAuthorizationHandler in the dependency injection container. Configure the policy with its requirement instances, then apply the policy to a Razor Page or action using [Authorize]. section 24.4.2
Listing 24.11 Registering the authorization handlers with the DI container
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization(options =>
options.AddPolicy(
"CanEnterSecurity",
policyBuilder => policyBuilder
.RequireClaim(Claims.BoardingPassNumber));
options.AddPolicy(
"CanAccessLounge",
policyBuilder => policyBuilder.AddRequirements(
new MinimumAgeRequirement(18),
new AllowedInLoungeRequirement()
));
});
services.AddSingleton<IAuthorizationHandler, MinimumAgeHandler>();
services.AddSingleton<IAuthorizationHandler, FrequentFlyerHandler>();
services.AddSingleton<IAuthorizationHandler, BannedFromLoungeHandler>();
services.AddSingleton<IAuthorizationHandler, IsAirlineEmployeeHandler>();
// Additional configuration
The registration example adds the lounge handlers to the DI container alongside the policy configuration. section 24.4.2
Use resource-based authorization when the decision depends on the particular item being protected. Create and register an AuthorizationHandler<TRequirement, TResource>, load the resource, and call IAuthorizationService.AuthorizeAsync with the user, resource, and policy. This allows a recipe-editing policy to check ownership rather than granting access to every recipe. section 24.5 section 25.6
Listing 25.3 Using resource authorization to protect a minimal API endpoint
app.MapDelete("recipe/{id}", async (
int id, RecipeService service,
IAuthorizationService authService, #1
ClaimsPrincipal user) => #2
{
var recipe = await service.GetRecipe(id); #3
var result = await authService.AuthorizeAsync( #4
user, recipe, "CanManageRecipe"); #4
if (!result.Succeeded) #5
{ #5
return Results.Forbid(); #5
} #5
await service.DeleteRecipe(id); #6
return Results.NoContent(); #6
});
#1 Injected to perform resource-based authorization
#2 The HttpContext.User claims principal for the request
#3 Fetches the recipe to access
#4 Performs resource-based authorization, passing in the user, resource, and the policy name
#5 If authorization failed, returns 403 Forbidden
#6 If authorization succeeded, executes the endpoint as normal
This minimal API endpoint loads a recipe and checks authorization before deleting it; an unsuccessful result returns Forbidden. section 25.6

