Intent.Security.JWT module configures JWT (JSON Web Token) Bearer authentication for your ASP.NET Core Web APIs, providing a complete token-based security infrastructure.
Overview
This module sets up JWT Bearer authentication middleware, configures token validation parameters, and integrates with the application’s security infrastructure. It provides the foundation for stateless, token-based API authentication.What Gets Generated
JWT Configuration
Configures JWT Bearer authentication in your application:Current User Service
Provides access to the authenticated user:Key Features
Bearer Authentication
Standard JWT Bearer token validation
Claims-Based
Access user identity and claims
Stateless
No server-side session storage required
Flexible Authorization
Support for roles, policies, and claims
Module Settings
JWT Bearer Authentication Type
Configure how JWT tokens are authenticatedOptions:
oidc- OpenID Connect JWT authentication (recommended)manual- Manual JWT authentication with custom validation
Configuration
appsettings.json
Configure JWT settings:Environment Variables
For production, use environment variables or Azure Key Vault:Authorization Strategies
Role-Based Authorization
Authorize based on user roles:Policy-Based Authorization
Define custom authorization policies:Custom Authorization Handlers
Implement custom authorization logic:Claims-Based Authorization
Authorize based on specific claims:Token Validation
The module configures comprehensive token validation:Validation Parameters
Custom Validation
Add custom token validation:Protecting Endpoints
Controller Level
Protect entire controller:Action Level
Protect specific actions:Using Current User Service
Access current user information in your services:Error Handling
Unauthorized (401)
Returned when:- No token provided
- Invalid token
- Expired token
- Token signature doesn’t match
Forbidden (403)
Returned when:- User is authenticated but lacks required permissions
- Missing required role
- Authorization policy not met
Security Best Practices
Secret Key Management
Secret Key Management
- Use a strong secret key (minimum 256 bits / 32 characters)
- Store secrets in Azure Key Vault or AWS Secrets Manager
- Never commit secrets to source control
- Use different secrets for each environment
- Rotate keys periodically
Token Lifetime
Token Lifetime
- Keep access tokens short-lived (15-60 minutes)
- Use refresh tokens for longer sessions
- Set ClockSkew to zero for strict expiration
- Implement token revocation for critical operations
HTTPS
HTTPS
- Always use HTTPS in production
- Set RequireHttpsMetadata to true
- Enable HSTS (HTTP Strict Transport Security)
- Use secure cookie flags
Claim Validation
Claim Validation
- Validate all claims on the server
- Don’t trust client-provided data
- Implement additional validation in TokenValidated event
- Check user status (active/disabled) on sensitive operations
OpenID Connect (OIDC) Authentication
When using OIDC mode:Installation
Dependencies
Intent.Application.Identity(>= 3.6.0)Intent.AspNetCore(>= 6.0.3)Intent.Common.CSharpIntent.OutputManager.RoslynWeaver
Integration
Automatically integrates with:Intent.IdentityServer4.SecureTokenServer- For IdentityServer4Intent.AspNetCore.Swashbuckle- Adds auth UI to SwaggerIntent.AspNetCore.Identity- For user management
Next Steps
Identity
Add user management with ASP.NET Core Identity
Identity JWT
Combine Identity with JWT authentication
Swashbuckle
Add JWT auth to Swagger UI
Controllers
Protect your API endpoints
