Wednesday, 20 November 2024

Claim in jwt authentication token

 In JWT (JSON Web Token) authentication, a claim is a piece of information that is encoded within the token. Claims represent statements about an entity (usually the user) and additional metadata. Claims are used to convey information that is relevant to the authentication or authorization process.

A JWT typically contains three parts: the header, the payload, and the signature. The claims are part of the payload.

Types of Claims in JWT

There are three types of claims in a JWT:

  1. Registered Claims: These are predefined claims that are not mandatory but recommended to use for common functionalities. Some of the registered claims include:

    • iss (Issuer): Identifies the principal that issued the JWT.
    • sub (Subject): Identifies the subject of the JWT (usually the user).
    • aud (Audience): Identifies the intended recipient(s) of the JWT.
    • exp (Expiration Time): The expiration time of the JWT, after which it should not be accepted.
    • nbf (Not Before): The time before which the token should not be accepted.
    • iat (Issued At): The time when the token was issued.
    • jti (JWT ID): A unique identifier for the JWT.
  2. Public Claims: These are custom claims that can be defined by anyone, but they should be registered in the IANA JSON Web Token Claims registry or be chosen carefully to avoid conflicts with other claims. These claims often contain information about the user, such as their roles, permissions, or other application-specific data.

  3. Private Claims: These are custom claims created to share information between the parties that agree on them. These are typically not registered or standardized, and they are meant to be used internally between the issuer and the consumer of the JWT.

Example of Claims in JWT Payload

Here is an example of a JWT payload with some claims:

{
    "iss": "example.com",         // Issuer
    "sub": "1234567890",           // Subject (user ID)
    "aud": "exampleApp",           // Audience
    "exp": 1625123456,             // Expiration time (timestamp)
    "iat": 1625113456,             // Issued at (timestamp)
    "role": "admin",               // Custom claim (e.g., user role)
    "username": "john_doe"         // Custom claim (e.g., username)
  }

In this example:

  • iss indicates the issuer of the token.
  • sub identifies the subject (user) of the token.
  • aud specifies the audience for whom the token is intended.
  • exp specifies when the token expires.
  • iat is the timestamp when the token was issued.
  • role and username are private, custom claims used in this specific application.

How Claims Are Used

  • Authentication: Claims like sub (subject) are used to identify the user or entity for which the token was issued.
  • Authorization: Claims like role can be used to check what actions the user is authorized to perform.
  • Token Integrity: Claims like exp (expiration) ensure that the token cannot be used after a certain time.

Claims allow JWT tokens to be versatile and carry various types of information that can be validated and used for access control, personalization, and ensuring the security of the token.


0 comments:

Post a Comment

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (47) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (55) CD (1) CI (2) CloudComputing (2) Coding (10) CQRS (1) CSS (2) Design_Pattern (7) DevOps (4) DI (3) Dotnet (10) DotnetCore (20) Entity Framework (5) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) jwtToken (4) Lamda (3) Linq (10) microservice (4) Mongodb (1) MVC (46) NodeJS (8) React (10) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (3) UI (1) UnitTest (2) WCF (14) Web Api (16) Web Service (1) XMl (1)

Dotnet Guru Archives