liveBook · Manning

Authorization requirement

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

An authorization requirement is a condition that must be satisfied for an authorization policy to succeed.

An authorization requirement is a condition that must be satisfied before an authorization policy succeeds. In ASP.NET Core, a policy has one or more requirements, and every requirement must be satisfied. A requirement can have multiple handlers, giving it more than one way to succeed. section 24.4.1

Authorization happens before a protected endpoint executes. Routing selects the endpoint, authentication supplies the request’s ClaimsPrincipal, and AuthorizationMiddleware checks the endpoint’s authorization metadata against that user. If authorization fails, the endpoint does not run. An unauthenticated user receives a challenge; an authenticated user who lacks permission receives a forbid response. section 24.2 section 24.2.2

Figure 24.6 For a policy to be satisfied, every requirement must be satisfied. A requirement is satisfied if any of the handlers is satisfied.
CH24_F06_Lock3

The diagram shows the two combination rules: all requirements in a policy must succeed, but any one handler can satisfy a requirement. section 24.4.1

Protecting an airline lounge

A lounge policy requires a minimum age of 18 and permission to enter. The age handler can calculate age from a DateOfBirth claim. Either a Gold frequent-flyer claim or an employee-number claim can satisfy the entry requirement. A banned-from-lounge claim can explicitly fail authorization, even if another handler reports success. section 24.4 section 24.4.2 section 24.7

To create a custom requirement:

  • Define a lightweight C# class implementing the marker interface IAuthorizationRequirement. Add a constructor property, such as a minimum age, when the condition needs a configurable value; requirements themselves do not use dependency injection. section 24.4.2
  • Add requirement instances to a named policy with AddRequirements(), then apply that policy to a Razor Page or action with [Authorize("CanAccessLounge")]. section 24.4.2
  • Implement and register AuthorizationHandler<T> handlers. Call context.Succeed(requirement) when a handler establishes success; otherwise, do nothing so another handler can succeed. Reserve context.Fail() for a condition that must override success. section 24.4.2 section 24.7

A simple claim check may need only RequireClaim() rather than custom classes. The same policies, requirements, and handlers also apply to minimal API endpoints, where a named policy can be attached with RequireAuthorization("CanCreate"). section 24.4 section 25.6

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

How deep it goes
Explains policy composition, custom marker classes, handler outcomes, registration, and explicit failure through a worked authorization policy.
Examples
Uses an airline-lounge policy requiring minimum age and either frequent-flyer or employee status, with a ban overriding access.
What it assumes
Builds on authentication and claims, then introduces requirements and handlers from their basic policy-building roles.
What you can do after
Build it from scratch with IAuthorizationRequirement, AuthorizationHandler<T>, and a named policy to protect an endpoint.
How it connects
Connects authorization policies to claims, ClaimsPrincipal, AuthorizationMiddleware, dependency injection, and resource-based authorization.

Also explained in

Compare how 2 books explain authorization requirement →

Related concepts

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.