Overview
AndanDo uses JSON Web Tokens (JWT) for API authentication and authorization. The JWT configuration controls token generation, validation, and expiration.JWT tokens are generated by the
JwtTokenService and used to authenticate users across API requests.Configuration
JWT settings are configured in theJwt section of appsettings.json:
appsettings.json
Configuration Properties
Issuer
The issuer (iss) claim identifies the principal that issued the JWT.
- Type:
string - Required: Yes
- Default:
"AndanDo" - Purpose: Identifies tokens issued by AndanDo
The issuer should match your application name or domain. This claim is validated when verifying tokens.
Audience
The audience (aud) claim identifies the recipients that the JWT is intended for.
- Type:
string - Required: Yes
- Default:
"AndanDo" - Purpose: Ensures tokens are only valid for AndanDo services
SecretKey
The secret key used to sign and verify JWT tokens using HMAC-SHA256.- Type:
string - Required: Yes
- Minimum Length: 32 characters (256 bits)
- Algorithm: HMAC-SHA256 (HS256)
ExpirationMinutes
The token expiration time in minutes.- Type:
int - Required: No
- Default:
60(defined inJwtOptions.cs:61) - Recommended:
120(2 hours)
JwtOptions Class
The configuration is bound to theJwtOptions class:
Services/JWT/JwtTokenService.cs
JwtTokenService Implementation
TheJwtTokenService generates JWT tokens based on the configuration:
Services/JWT/JwtTokenService.cs
Token Claims
Each generated token includes the following claims:| Claim | Description | Example |
|---|---|---|
sub | Subject - User ID | "12345" |
email | User’s email address | "[email protected]" |
jti | JWT ID - Unique token identifier | "3fa85f64-5717-4562-b3fc-2c963f66afa6" |
iss | Issuer | "AndanDo" |
aud | Audience | "AndanDo" |
nbf | Not Before - Token valid start time | 1709744400 |
iat | Issued At - Token creation time | 1709744400 |
exp | Expiration - Token expiry time | 1709751600 |
Claims are encoded in the JWT payload and can be read without validation, but cannot be tampered with due to the signature.
Service Registration
The JWT service is registered inProgram.cs:
Program.cs
Usage Example
Generating a JWT token for a user:Environment-Specific Configuration
Development (User Secrets)
Development (User Secrets)
Store JWT settings in User Secrets for development:Verify secrets:
Production (Environment Variables)
Production (Environment Variables)
Use environment variables in production:Or in
appsettings.Production.json:Azure App Service / Key Vault
Azure App Service / Key Vault
Store the secret key in Azure Key Vault:Reference in App Service configuration:
Testing JWT Configuration
Verify JWT configuration is working:Troubleshooting
SecurityTokenInvalidSignatureException
SecurityTokenInvalidSignatureException
Error: Token signature validation failedCause: The
SecretKey used to sign the token doesn’t match the key used to validate it.Solutions:- Ensure the same
SecretKeyis used across all instances - Verify no extra whitespace in the configuration
- Check environment-specific configuration is loading correctly
- Verify the secret key is at least 32 characters
SecurityTokenExpiredException
SecurityTokenExpiredException
Error: Token has expiredCause: The current time is past the token’s
exp claim.Solutions:- Increase
ExpirationMinutesif tokens expire too quickly - Implement token refresh mechanism
- Ensure server clocks are synchronized (NTP)
- Check for time zone issues
Argument exception: Key length too short
Argument exception: Key length too short
Error: IDX10603: The algorithm requires the SecurityKey.KeySize to be greater than 256 bitsCause: The
SecretKey is less than 32 characters (256 bits).Solution: Use a secret key with at least 32 characters:Configuration section 'Jwt' not found
Configuration section 'Jwt' not found
Error: Options are empty or not boundSolutions:
- Verify
appsettings.jsoncontains theJwtsection - Check
Program.csincludes the configuration binding - Ensure environment-specific files are properly named
- Verify JSON syntax is valid