Security Guidelines and Best Practices
🔐 Critical Security Requirements
Key Length : JWT secret key MUST be at least 32 characters long
Key Strength : Avoid default, fallback, or sample keys in production
Key Management : Store JWT keys in secure environment variables only
Token Validation : All JWT validation failures are logged for security monitoring
Token Headers : Multiple headers supported (Authorization, X-Forwarded-Authorization, etc.)
# Example: Generate secure JWT key
JWT_SECRET_KEY=$( openssl rand -base64 32)
Connection Resilience : Enhanced retry policies with exponential backoff
Connection Timeout : 120-second timeout for database operations
Retry Logic : 5 attempts with up to 30-second delays
Error Handling : Specific SQL error codes handled for Azure SQL and on-premises
Connection Pooling : EF Core manages connection pooling automatically
Authentication Flow Security
OAuth2 PKCE : Proof Key for Code Exchange mandatory for OAuth2 flows
Session Management : Redis-based session storage with TTL expiration
Token Lifecycle : Short-lived access tokens (15min) + long-lived refresh tokens (30 days)
Session Validation : Real-time session validation for critical operations
Logout Security : Proper token revocation and cleanup
Input Validation Security
Data Annotations : Comprehensive validation rules on all DTOs
Business Logic Validation : Domain-specific validation in Application layer
SQL Injection Prevention : Entity Framework parameterized queries
XSS Protection : JSON serialization prevents script injection
CSRF Protection : OAuth2 state parameter validation
🛡️ Production Security Checklist
Environment Configuration
Runtime Security Monitoring
🚨 Security Incident Response
Immediate : Rotate JWT secret key
Revoke : All existing refresh tokens in Redis
Audit : Review authentication logs for suspicious activity
Monitor : Enhanced logging for unusual patterns
Database Security Incident
Isolate : Database connections if breach suspected
Audit : Query logs for unauthorized access patterns
Verify : Data integrity and unauthorized modifications
Recovery : Implement additional connection restrictions
Session Security Incident
Clear : All Redis sessions for affected users
Force : Re-authentication for all users
Monitor : Session creation patterns
Update : Session validation logic if needed
📊 Security Monitoring and Logging
Critical Security Events to Monitor
JWT token validation failures
Database connection retries and failures
WebSocket connection anomalies
OAuth2 authentication failures
Session validation failures
Credit balance tampering attempts
Log Levels for Security Events
// Security violations - ERROR level
_logger . LogError ( "Security violation detected: {Details}" , details ) ;
// Authentication failures - WARNING level
_logger . LogWarning ( "Authentication failed: {Reason}" , reason ) ;
// Security success events - INFORMATION level
_logger . LogInformation ( "Secure operation completed: {Operation}" , operation ) ;
// Security debugging - DEBUG level (development only)
_logger . LogDebug ( "Security check passed: {Check}" , check ) ;
🔧 Development Security Guidelines
Never hardcode secrets in source code
Validate all inputs at API and business logic layers
Use parameterized queries exclusively (EF Core handles this)
Handle exceptions securely without exposing system details
Log security events appropriately for monitoring
Testing Security Features
Authentication Testing : Valid/invalid tokens, expired tokens, malformed tokens
Authorization Testing : Role-based access, resource ownership
Input Validation Testing : Boundary conditions, malformed data
Session Testing : Session hijacking prevention, timeout handling
Error Handling Testing : Information leakage prevention
🎯 Recent Security Improvements
JWT Security Enhancements
Added JWT key length validation (minimum 32 characters)
Implemented fallback key detection and prevention
Enhanced JWT validation exception handling and logging
Added structured logging for security event tracking
Database Connection Security
Increased retry attempts from 3 to 5
Extended maximum retry delay from 10 to 30 seconds
Added 10 specific SQL error codes for better failure handling
Set 120-second command timeout for long operations
WebSocket Security Improvements
Enhanced connection lifecycle management
Added proper resource cleanup on disconnection
Implemented heartbeat mechanism for connection health
Added connection timeout handling (30 minutes)
Session Management Security
Implemented real-time session validation
Added session activity tracking
Graceful handling of session storage failures
Enhanced session-based authentication for critical operations