🟢 1. What is JWT?
-
JWT stands for JSON Web Token.
-
It is a secure way to transmit information between a client (like a browser) and a server.
-
The information is digitally signed so it cannot be tampered with.
Analogy:
-
Think of JWT like a sealed envelope with a message inside.
-
Only the server can verify the seal (signature) to know it’s authentic.
🟢 2. Structure of JWT
A JWT is a string divided into 3 parts separated by dots (.
):
-
Header – says what type of token it is and the algorithm used.
-
Payload – the actual information (claims). Example:
-
Signature – a hash created from header + payload + secret key.
-
Ensures the token is authentic and unchanged.
-
🟢 3. How JWT Works
-
User logs in with username/password.
-
Server verifies credentials.
-
Server generates JWT (with user info in payload) and sends it to client.
-
Client stores JWT (usually in local storage or cookies).
-
For future requests, client sends JWT in HTTP header:
-
Server verifies JWT signature. If valid, user is authenticated.
🟢 4. JWT in C# (.NET Core / .NET 5+)
Step 1: Install NuGet package
Step 2: Create a JWT
Step 3: Validate JWT
🟢 5. Important Points
-
Keep secret key safe! If leaked, attackers can create valid tokens.
-
Do not store sensitive info like passwords in the payload. JWT is only base64 encoded, not encrypted.
-
Expiration is important. Never issue a token without expiry.
-
Bearer token in HTTP header is standard.
🟢 Summary
-
JWT = Header + Payload + Signature
-
Used for authentication and authorization
-
Signed so server can trust it
-
In C#, use System.IdentityModel.Tokens.Jwt to create & verify tokens