liveBook · Manning

IAuthorizationRequirement

As explained in ASP.NET Core in Action, 3e

IAuthorizationRequirement is the ASP.NET Core marker interface implemented by classes that represent authorization requirements.

IAuthorizationRequirement is a blank ASP.NET Core marker interface for classes that represent authorization requirements. Requirement classes are usually lightweight POCOs: they identify what a policy must satisfy, while authorization handlers contain the decision logic. section 24.4.2

A requirement can be modeled in two common ways:

  • Create requirement instances manually when adding them to a policy; do not use dependency injection in the requirement itself. Put injected dependencies in handlers instead. section 24.4.2

Combining requirements in a policy

builder.Services.AddAuthorization(options => { options.AddPolicy("CanAccessLounge", policyBuilder => policyBuilder.AddRequirements( new MinimumAgeRequirement(18), new AllowedInLoungeRequirement())); }); Each requirement represents one condition that must be satisfied for the policy to succeed. Handlers can call context.Succeed(requirement) when their condition is met or context.Fail() when access must be denied.

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines the marker interface, empty and parameterized implementations, policy composition, and the boundary between requirements and handlers.
Examples
Uses AllowedInLoungeRequirement, MinimumAgeRequirement, IsRecipeOwnerRequirement, and the CanAccessLounge and CanManageRecipe policies.
What it assumes
Assumes basic C# classes and ASP.NET Core authorization concepts, while teaching the requirement interface from its empty marker contract.
What you can do after
Build it from scratch by defining requirement classes, adding instances to policies, and delegating evaluation to registered handlers.
How it connects
Connects authorization policies, authorization handlers, dependency injection, and IAuthorizationService for resource-based authorization.

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.