ClaimsPrincipal, ClaimsIdentity, Claims explained in dotnet authentication
- Authentication means identifying a subject (user, external application etc)
- ASP.Net Core Identity is a framework that uses claims based authentication to authenticate users
Key concepts in Claims-based authentication
Claim
- Claim is a statement about the subject (user) being authenticated
- It is a key value pair (like username=John is a claim, email=john1543@gmail.com is a claim, userId=236 is a claim)
- A claim can have an issuer (for example: userId=236 as per system, date of birth=1st-Jan-2006 as per Gmail; here system and Gmail are the issuers)
ClaimsIdentity
- ClaimsIdentity is a collection of claims of the subject (user)
- For example, driving license of a subject (user) can be a ClaimsIdentity that has claims like name, photo, date of birth, address etc. Passport can be a ClaimsIdentity that has claims like name, place of birth, photo, nationality, permanent address etc
- ClaimsIdentity also has a string property called AuthenticationType which specifies how the ClaimsIdentity was authenticated for user.
- For example, a Passport (ClaimsIdentity) can be authenticated by matching the user’s face. Here matching the user’s face is the AuthenticationType
- In ASP.Net Core Identity, the AuthenticationType for a ClaimsIdentity can be Password, Windows authentication, Two Factor authentication, federated authentication etc.
ClaimsPrincipal
- It represents the subject (like a user) which is being authenticated
- It can have one or more ClaimsIdentities
- For example, a person (ClaimsPrincipal) can have driving license (ClaimsIdentity), passport (ClaimsIdentity), Voter ID card (ClaimsIdentity)
Relation between ClaimsPrincipal, ClaimsIdentity, Claims
- A person (ClaimsPrincipal) can have multiple ClaimsIdentities (like passport, driving license, Voter ID etc). Each ClaimsIdentity contains one or more claims about the user (like name, phone number, date of birth, nationality etc.)
ClaimsPrincipal in Cookie
- User will login providing his subject id like username/email and completing the required login challenge like password, 2 factor etc.
- User details will be retrieved from the users database and a ClaimsPrincipal will be created for the user
- The ClaimsPrincipal will be encoded (in base64) and then encrypted and this content would be kept in a cookie
- Every time the application is accessed, the ClaimsPrincipal will be retrieved from the cookie and user is authenticated (identified)
References
- Overview of dotnet identity - https://learn.microsoft.com/en-us/aspnet/core/security/authentication/?view=aspnetcore-9.0
- Claims Principal - https://learn.microsoft.com/en-us/dotnet/api/system.security.claims.claimsprincipal?view=net-9.0#properties
- Claims Identity - https://learn.microsoft.com/en-us/dotnet/api/system.security.claims.claimsidentity?view=net-9.0#properties
- Claim - https://learn.microsoft.com/en-us/dotnet/api/system.security.claims.claim?view=net-9.0#properties
- Claims principal explained with real world examples - https://andrewlock.net/introduction-to-authentication-with-asp-net-core/
- What is stored in Dotnet cookies - https://nestenius.se/net/exploring-what-is-inside-the-asp-net-core-cookies/
- ClaimsPricipal expalined - https://dev.to/pbouillon/understanding-identity-in-net-2169
- https://www.reddit.com/r/dotnet/comments/we9qx8/a_comprehensive_overview_of_authentication_in/
Comments
Post a Comment