Overview
The authentication system provides:- JWT Access Tokens: Short-lived tokens for API authentication
- Refresh Tokens: Long-lived tokens for obtaining new access tokens
- Token Rotation: Automatic refresh token rotation on renewal
- Security Auditing: Login attempts and token issuance tracking
- Session Management: Track active user sessions
Configuration
Configure JWT settings inappsettings.json:
appsettings.json
JwtOptions Properties
The issuer claim identifies the principal that issued the JWT. Typically your application or domain name.
The audience claim identifies the recipients that the JWT is intended for.
The secret key used to sign JWTs. Must be at least 32 characters long. Keep this secure and use environment variables in production.
Access token expiration time in minutes. Shorter durations are more secure.
Refresh token expiration time in days.
The
JwtOptions class validates configuration on startup. The signing key must be at least 32 characters, and issuer/audience cannot be empty.Token Generation
Generate Access Token
The token generation endpoint authenticates users and returns JWT tokens.POST /api/v1/identity/token
Token Generation Flow
TheGenerateTokenCommandHandler performs the following steps:
Validate Credentials
Validates the user’s email and password using
IIdentityService.ValidateCredentialsAsync().Security Audit
Logs the login attempt (success or failure) using
ISecurityAudit for compliance and monitoring.Issue Tokens
Generates JWT access token and cryptographic refresh token using
ITokenService.IssueAsync().Store Refresh Token
Persists the hashed refresh token in the database for validation during token refresh.
Token Service Implementation
TheTokenService generates JWT tokens with user claims:
TokenService.cs
Token Refresh
Refresh tokens allow clients to obtain new access tokens without re-authenticating. Endpoint:POST /api/v1/identity/token/refresh
Refresh Token Flow
Validate Refresh Token
Checks if the refresh token exists, is not expired, and matches the stored hash.
Using JWT Tokens
Include the access token in theAuthorization header for authenticated requests:
Security Features
Security Auditing
All login attempts, token issuance, and refresh operations are audited with IP address, user agent, and timestamps.
Token Fingerprinting
Tokens are never stored in plaintext. Only SHA-256 fingerprints are persisted for audit trails.
Session Management
Active sessions are tracked per user, allowing administrators to view and revoke sessions.
Refresh Token Rotation
Each refresh token can only be used once, preventing replay attacks.
Integration Events
Successful token generation publishes aTokenGeneratedIntegrationEvent to the outbox:
- Analytics and monitoring
- User activity tracking
- Fraud detection
- Compliance reporting
Best Practices
Use Short Access Token Lifetimes
Use Short Access Token Lifetimes
Keep access tokens short-lived (2-15 minutes) to minimize the impact of token theft. Use refresh tokens to obtain new access tokens.
Secure the Signing Key
Secure the Signing Key
Store the JWT signing key in environment variables or a secure secret manager. Never commit it to source control.
Implement Token Revocation
Implement Token Revocation
While JWTs are stateless, maintain a blacklist or use session management to revoke tokens when needed.
Use HTTPS Only
Use HTTPS Only
Always transmit tokens over HTTPS to prevent man-in-the-middle attacks.
Related Topics
Authorization
Learn about role-based and permission-based authorization
Multi-Tenancy
Understand how authentication works with multi-tenant applications
Rate Limiting
Protect authentication endpoints with rate limiting
Observability
Monitor authentication metrics and traces
