Overview
AuthService implements account lockout to protect against brute-force password attacks. After 5 failed login attempts, accounts are temporarily locked for 15 minutes.Implementation Details
Lockout Configuration
The lockout parameters are configured as constants in the AuthService:Services/AuthService.cs
| Parameter | Value | Rationale |
|---|---|---|
| Max Failed Attempts | 5 | Balances security and user experience; legitimate users rarely exceed this |
| Lockout Duration | 15 minutes | Long enough to slow brute-force attacks, short enough to not frustrate users |
User Entity Schema
Lockout state is tracked directly in the User entity:Entities/User.cs
FailedLoginAttempts- Counter incremented on each failed loginLockoutEnd- Timestamp when the lockout expires (null if not locked)IsLockedOut()- Computed property checking if lockout is currently active
Using a nullable
DateTime? for LockoutEnd allows us to distinguish between “never locked” (null) and “locked until X time” (has value). This is more efficient than storing separate boolean flags.Login Flow with Lockout
Step 1: Check Account Status
Before verifying the password, the service checks if the account is locked:Services/AuthService.cs
The lockout check happens before password verification. This prevents attackers from using password verification timing to determine if an account is locked.
Step 2: Handle Failed Attempts
When password verification fails, the failed attempt counter is incremented:Services/AuthService.cs
- Increment
FailedLoginAttemptscounter - If counter reaches threshold (5), set
LockoutEndto 15 minutes from now - Log the lockout event for security monitoring
- Save changes to database
- Return generic error message
Step 3: Reset on Successful Login
When a login succeeds, the lockout state is cleared:Services/AuthService.cs
Security Rationale
Generic Error Messages
The implementation uses generic error messages to prevent user enumeration:User Enumeration Attack: An attacker tries to determine which email addresses have accounts in the system by observing different error messages. With generic messages, all failed logins look identical, making enumeration much harder.
Why Show Lockout Status?
You might notice that when an account IS locked, we show a specific message:- The attacker already knows the account exists (they triggered the lockout)
- Telling legitimate users about the lockout improves UX
- It still doesn’t reveal the password or whether previous attempts were close
- The lockout itself stops the attack
Lockout Duration Trade-offs
| Duration | Security | User Experience |
|---|---|---|
| 5 minutes | Weaker protection, ~12 attempts/hour | Better for legitimate users |
| 15 minutes | Good balance, ~4 attempts/hour | Acceptable frustration |
| 30 minutes | Stronger protection, ~2 attempts/hour | May frustrate users |
| Permanent | Requires manual unlock or email reset | Poor UX, DoS risk |
Attack Scenarios
Brute-Force Attack
Without Lockout:Distributed Attack
Account Lockout DoS
Attack: An attacker intentionally locks out legitimate users by making 5 failed login attempts per account. Mitigations:- Temporary lockout - 15 minutes is annoying but not critical
- Generic messages - Attacker can’t confirm if lockout succeeded
- Monitoring - Unusual lockout patterns trigger alerts
- Password reset - Users can bypass via email verification
Monitoring and Logging
The system logs lockout events for security monitoring:Services/AuthService.cs
- Spike in lockout events (possible attack)
- Same IP causing multiple lockouts (distributed attack)
- Lockouts on admin/service accounts (targeted attack)
- Unusual geographic patterns
Integrate these logs with a Security Information and Event Management (SIEM) system for real-time threat detection and automated response.
Best Practices
Configuration
- Adjust for your threat model - High-security applications may use 3 attempts / 30 minutes
- Consider account value - Admin accounts might have stricter limits
- Make it configurable - Use configuration files instead of constants for production
User Experience
- Show remaining time - Help legitimate users know when to retry
- Offer password reset - Provide an escape hatch for locked users
- Email notification - Alert users when their account is locked (possible compromise)
- Multi-factor authentication - Stronger than lockout alone
Additional Security Layers
- Rate limiting - Limit requests per IP address
- CAPTCHA - After 2-3 failed attempts
- IP reputation - Block known malicious IPs
- Device fingerprinting - Detect suspicious login patterns
- Anomaly detection - Machine learning to identify attacks
Testing Lockout Behavior
To test the lockout mechanism:Related Topics
- Password Hashing - How passwords are securely stored
- Token Validation - JWT security after successful login
- Authentication Flow - Complete login process
- Error Handling - API error responses