ClaimsPrincipal, ClaimsIdentity, Claims explained in dotnet authentication

claimsprincipal_dotnet
  • 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.)

image.png

image.png

  • 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)

image.png

image.png

References

Comments

Popular posts from this blog

Implicit usings, top level statements, file scoped namespaces in dotnet

Introduction to dotnet with a simple hello world example

Setup dotnet development in Visual Studio, dotnet CLI and VS code