liveBook · Manning

Claims-based access control

As explained in Building Web APIs with ASP.NET Core

Claims-based access control is an authorization model that evaluates claims associated with an authenticated entity, optionally including their values, to determine access.

Claims-based access control (CBAC) authorizes an authenticated user by checking claims attached to that user, including claim values when needed. A claim can represent a role, mobile-phone number, date of birth, or another relevant property. Role-based access control (RBAC) is effectively a simpler CBAC form that checks only the role claim. section 9.4.7

Implement CBAC through named authorization policies:

  • Declare each policy explicitly in Program.cs because claim requirements are policy-based rather than exposed through the Authorize attribute’s Role property. section 9.4.7
  • Combine requirements such as the Moderator role and the presence of a mobile-phone claim in one policy. section 9.4.7
  • Apply the named policy to an endpoint with [Authorize(Policy = "ModeratorWithMobilePhone")]. section 9.4.7
  • Support the policy by accepting and storing the mobile-phone value, then conditionally adding it to the JWT as a claim during login. section 9.4.7
builder.Services.AddAuthorization(options => 
{
    options.AddPolicy("ModeratorWithMobilePhone", policy =>
        policy
            .RequireClaim(ClaimTypes.Role, RoleNames.Moderator)#1
            .RequireClaim(ClaimTypes.MobilePhone));             #2
});

The policy configuration shows how CBAC requires both a specific role claim and a mobile-phone claim before authorization succeeds. section 9.4.7

Checking a minimum age

A policy can use a custom assertion instead of only checking claim presence. The MinAge18 example requires a date-of-birth claim, parses its value, compares it with the current date, and protects an endpoint through [Authorize(Policy = "MinAge18")]. section 9.4.7

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains policy registration, compound requirements, endpoint application, and assertion-based value checks.
Examples
Demonstrates ModeratorWithMobilePhone and MinAge18 policies using mobile-phone, role, and date-of-birth claims.
What it assumes
Assumes authenticated users, JWT claims, the Authorize attribute, and basic ASP.NET Core service configuration.
What you can do after
Build and apply named ASP.NET Core authorization policies that combine claim presence, claim values, and custom assertions.
How it connects
Connects claims with RBAC, PBAC, authorization policies, JWT tokens, and ASP.NET Core Identity.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.