liveBook · Manning

Authorization requirement

As explained in .NET in Action, Second Edition

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 for an authorization policy to succeed. ASP.NET Core represents custom requirements with IAuthorizationRequirement and evaluates them through authorization handlers. section 9.6

Use requirements to express policy conditions and keep their runtime behavior explicit:

  • Attach a requirement to a named policy alongside RequireAuthenticatedUser and the required authentication scheme. section 9.6
  • Implement the condition in an AuthorizationHandler<TRequirement>, inspecting claims or other authorization context. section 9.6
  • Call context.Succeed(requirement) only when the condition is satisfied; leaving it unresolved allows other handlers to succeed it. section 9.6
  • Avoid marking failure unless the requirement must explicitly fail, because conflicting handler outcomes leave the requirement failed. section 9.6

A claim-based OnlyMe requirement

Define OnlyMeRequirement as IAuthorizationRequirement and register it with the OnlyMe policy. Its handler finds the preferred_username claim and calls context.Succeed(requirement) when the value matches the permitted account. Apply the policy to create and update actions. Unauthenticated requests receive 401; authenticated requests that fail the requirement receive 403. A hardcoded email is suitable only for demonstrating the mechanism: production values should come from a safe, verified source. section 9.6

How .NET in Action, Second Edition covers it

How deep it goes
Explains registration, handler execution, claim checks, multiple-handler outcomes, and explicit failure behavior, but not broader policy-engine internals.
Examples
Uses an OnlyMe requirement that compares the preferred_username claim with a permitted email address.
What it assumes
Assumes familiarity with authenticated users, claims, named policies, and authentication schemes.
What you can do after
Apply it via ASP.NET Core policies and handlers to protect API operations with custom authorization conditions.
How it connects
Connects authorization policies, authorization handlers, authentication schemes, claims, and bearer-token authentication.

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 .NET in Action, Second Edition. The section links point to the parts of the book it draws on.