← Foundations path
.NETFoundations 27 / 37

Claims and ClaimsPrincipal

“What is a claim, and what is a ClaimsPrincipal in ASP.NET Core?”

How the API knows who is calling: a list of small facts about the user, rebuilt from the token on every request.

See it

1/8A request arrives with a token. Inside: claims about Priya, a credit officer at dealer D-204.

In plain words

When someone signs in, the sign-in service (at our clients, Entra ID, Microsoft's sign-in service) gives their app an access token: a signed card the app shows on every call to the API. The signature proves nobody edited the claims after the service wrote them (). The token carries small facts about the person: their id, their name, their roles, which dealer they work for. Each fact is a claim: a name and a value, like role = CreditOfficer or dealer_id = D-204. Two jobs use claims. Authentication means proving who you are: on every request, the authentication middleware checks the token and copies its claims into an object called a , the signed-in user as .NET sees them. Authorization means deciding what that user may do: roles and policies read the claims (a policy is a named rule, see ). You find the principal at , or just inside a controller. Your code asks it questions: what is your name, are you in this role, which dealer are you from. The API does not look the user up in a database to answer: everything it knows is in those claims. Inside the principal sits a : the claims that came from one source (here, one token) plus a label for how they were proven (its authentication type; for a token, the JWT handler inside the middleware sets it). The identity counts as signed in only when it has that label. The principal is rebuilt from the token on every request. Nothing is kept between requests, unlike a login session.

Say it in the interview

In the lending API

A controller that reads the caller's claims. Tap Next line to walk it.

Lending.sln · where this file sits
  • src/Api/Controllers/
Diagram · step 1A request arrives with a token. Inside: claims about Priya, a credit officer at dealer D-204.
MeController.cs · line 1 · 1 of 11Marks this class as a web API controller and turns on automatic checks ().

What they ask next

Interviewers check that you can explain every line. Tap a question to open it.

Traps

Read more: Claims-based authorization in ASP.NET Core · ClaimsPrincipal class