liveBook · Manning

Claim

As explained in Building Web APIs with ASP.NET Core

A claim is a statement about an authenticated user or principal, represented as typed identity data and optionally carrying a value.

A claim is a typed piece of information about an authenticated user. In the board-game API, claims travel in the user’s JSON Web Token (JWT). The authorization process reads them to decide whether a request meets an endpoint’s access rules. section 9.2.6 section 9.4.4

Start with the login flow. After checking the username and password, the Login action creates a list of claims. Its first claim uses ClaimTypes.Name and the user’s UserName. The action then combines the claims with signing credentials, issuer, audience, and expiration time to create a JWT. It returns the token string so the client can send it in the Authorization header on later requests. section 9.2.6

A name claim identifies the user, but it does not say whether that user is a moderator or administrator. For role-based access control, add a ClaimTypes.Role claim for each role returned by UserManager.GetRolesAsync(user). A user’s token can therefore have no role claims, one, or several. Authorization rules on controllers or actions can then check those roles. section 9.4 section 9.4.4

// ... existing code
 
var claims = new List<Claim>();
claims.Add(new Claim(
    ClaimTypes.Name, user.UserName));
claims.AddRange(
(await _userManager.GetRolesAsync(user))
.Select(r => new Claim(ClaimTypes.Role, r)));
 
// ... existing code

The login snippet keeps the name claim and turns each assigned role into a separate role claim. section 9.4.4

Role is only one possible claim type. A requirement could also check for ClaimTypes.MobilePhone. This is claims-based access control: it can test a claim or a combination of claims, while role-based access control checks the role claim. Role requirements can use the Roles property of [Authorize]; other claim requirements need a policy registered in Program.cs. section 9.4.7

Require a moderator with a mobile-phone claim

Register a policy that requires both the Moderator role claim and the presence of a mobile-phone claim, then put [Authorize(Policy = "ModeratorWithMobilePhone")] on the protected action. For this to work, registration must accept and save the phone number, and login must add its claim when the number is present. This is an extension to implement, not a claim supplied by the role-only login flow. 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 RequireClaim combines a specific role value with the presence of another claim type. section 9.4.7

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Works through name and role claim creation, then outlines a mobile-phone policy without implementing its required registration and login changes.
Examples
Uses a username claim, per-role claims, and a proposed policy requiring both Moderator and mobile-phone claims.
What it assumes
Assumes an ASP.NET Core login action and JWT setup; introduces how its claim list supports authorization decisions.
What you can do after
Apply it via ASP.NET Core Identity and authorization policies to place role claims in JWTs and specify claim requirements.
How it connects
Connects claims to authentication, JWTs, role-based access control, claims-based access control, and authorization policies.

Also explained in

Compare how 7 books explain claim →

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.