Part 4: Tokens and Token Management
The Token That Broke Production
The Three Token Types
1. Access Tokens
// Access token example (decoded for illustration)
{
"aud": "api://payment-api", // Audience: who can use this token
"iss": "https://login.microsoftonline.com/{tenant}/v2.0", // Issuer
"iat": 1708185600, // Issued at
"nbf": 1708185600, // Not before
"exp": 1708189200, // Expires (1 hour later)
"sub": "user-object-id", // Subject: user ID
"oid": "user-object-id", // Object ID (same as sub in v2)
"preferred_username": "[email protected]",
"scp": "Payments.Read Payments.Write", // Scopes granted
"appid": "frontend-client-id", // Application that requested token
"ver": "2.0" // Token version
}2. ID Tokens
3. Refresh Tokens
JWT Structure Deep Dive
Header
Payload (Claims)
Signature
Token Validation
Validation Checklist
Manual Validation Example
Using Microsoft Libraries
JWKS (JSON Web Key Set)
JWKS Endpoint
JWKS Response
Caching JWKS
Key Rotation
Token Lifetimes
Default Lifetimes
Configuring Lifetimes
Token Lifetime Policies (PowerShell)
Refresh Token Behavior
Access Token vs ID Token: When to Use What
Access Token
ID Token
Token Storage
Frontend Storage
Backend Storage
Token Claims and Customization
Optional Claims
App Roles
Groups Claims
Common Pitfalls
Pitfall 1: Not Caching JWKS
Pitfall 2: Trusting Decoded Tokens
Pitfall 3: Logging Tokens
Pitfall 4: Wrong Token for Purpose
Key Takeaways
What's Next
Last updated