In ASP.NET Core Identity, what do the default Password and Lockout options enforce, and why can lockout still never trigger on a login page?
answer
- six characters, four character classes
- 5 failures, 5 minutes
- AllowedForNewUsers sets LockoutEnabled
- lockoutOnFailure is a per-call switch
- the default UI passes false
basics
~20 sDefaults: passwords of at least 6 characters with a digit, lowercase, uppercase and non-alphanumeric character; lockout after 5 failures for 5 minutes. Failures count only when PasswordSignInAsync gets lockoutOnFailure: true, which the default login pages do not pass.
solid answer
~40 s`IdentityOptions.Password` defaults to `RequiredLength = 6`, `RequiredUniqueChars = 1` and `RequireDigit`, `RequireLowercase`, `RequireUppercase` and `RequireNonAlphanumeric` all `true`; `PasswordValidator<TUser>` enforces them in `CreateAsync`, `ChangePasswordAsync` and `ResetPasswordAsync`. `IdentityOptions.Lockout` defaults to `MaxFailedAccessAttempts = 5`, `DefaultLockoutTimeSpan = 5 minutes` and `AllowedForNewUsers = true`, which sets `LockoutEnabled` on users when they are created. Failures are only counted when the caller asks: `PasswordSignInAsync(..., lockoutOnFailure: true)`. The default Identity UI's Login page and the Blazor template's Login component both pass `false`, with a comment saying to change it, so a stock app never locks anyone out. Users created while `AllowedForNewUsers` was off also have `LockoutEnabled = false` and are never treated as locked.
code
csharp · 11 lines// Scaffolded Login page, as generated
var result = await _signInManager.PasswordSignInAsync(
Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
// What a site that relies on lockout must call instead
var checkedResult = await _signInManager.PasswordSignInAsync(
Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: true);
if (checkedResult.IsLockedOut)
{
return RedirectToPage("./Lockout");
}go deeper
Recall the defaults: 6-character passwords with digit, lower, upper and symbol; lockout after 5 failures for 5 minutes.
Explain the lockout mechanics — AccessFailedAsync, LockoutEnd, LockoutEnabled — and that lockoutOnFailure decides whether failures count.
Find why lockout never fires in a real app: the scaffolded false argument, LockoutEnabled off on old users, a store without lockout support, or direct CheckPasswordAsync calls.
Balance lockout against denial of service on known user names, and decide where attempt limiting belongs between Identity, rate limiting and the edge.
## Where the options live All of ASP.NET Core Identity's behaviour switches hang off `IdentityOptions`, configured through the setup action of `AddIdentity`, `AddDefaultIdentity` or `AddIdentityCore`, or with `services.Configure<IdentityOptions>(...)`. Two groups govern credentials. ## Password options | `IdentityOptions.Password` | Default | |---|---| | `RequiredLength` | 6 | | `RequiredUniqueChars` | 1 | | `RequireDigit` | `true` | | `RequireLowercase` | `true` | | `RequireUppercase` | `true` | | `RequireNonAlphanumeric` | `true` | `PasswordValidator<TUser>` applies them whenever a password is set: `UserManager.CreateAsync(user, password)`, `ChangePasswordAsync`, `ResetPasswordAsync` and `AddPasswordAsync`. Violations come back as `IdentityError` codes such as `PasswordTooShort` or `PasswordRequiresNonAlphanumeric`. Whether these composition rules are a good policy is a separate design question; many teams raise `RequiredLength` and relax the character classes. Adding a breached-password check means registering another `IPasswordValidator<TUser>`, since validators run in sequence. ## Lockout options | `IdentityOptions.Lockout` | Default | |---|---| | `MaxFailedAccessAttempts` | 5 | | `DefaultLockoutTimeSpan` | 5 minutes | | `AllowedForNewUsers` | `true` | The mechanics live in `UserManager` and `SignInManager`: 1. A failed password check calls `UserManager.AccessFailedAsync`, **only if** the caller passed `lockoutOnFailure: true` and the store supports lockout. 2. `AccessFailedAsync` increments `AccessFailedCount`. When it reaches `MaxFailedAccessAttempts`, it sets `LockoutEnd` to now plus `DefaultLockoutTimeSpan` and resets the count. 3. `IsLockedOutAsync` returns `true` only if the user's `LockoutEnabled` flag is set **and** `LockoutEnd` is in the future. 4. The attempt that crosses the threshold already returns `SignInResult.LockedOut`; later attempts get `LockedOut` from the pre-sign-in check before the password is even checked. 5. A successful password check resets the count (deferred until the second factor when two-factor is required). ## Why lockout silently never happens - **`lockoutOnFailure: false`.** The scaffolded Identity UI `Login` page and the Blazor Web App template's `Login` component both call `PasswordSignInAsync(..., lockoutOnFailure: false)`, with the comment "To enable password failures to trigger account lockout, set lockoutOnFailure: true". Raising `MaxFailedAccessAttempts` or shortening `DefaultLockoutTimeSpan` changes nothing until that argument changes. (`MapIdentityApi`'s `/login` passes `true`.) - **`LockoutEnabled` is false on the user.** `AllowedForNewUsers` is applied at creation only. Accounts imported or created while it was `false` keep `LockoutEnabled = false`; `UserManager.SetLockoutEnabledAsync(user, true)` fixes them. - **A custom store without `IUserLockoutStore<TUser>`.** `SupportsUserLockout` is then `false` and nothing is counted. - **`CheckPasswordAsync` used directly.** It only compares hashes; counting happens in `SignInManager.CheckPasswordSignInAsync` and `PasswordSignInAsync`. ## Configuring the garden site The plot booking site wants lockout to work and stronger passwords: ```csharp builder.Services.AddIdentity<GardenUser, IdentityRole>(o => { o.Password.RequiredLength = 12; o.Password.RequireNonAlphanumeric = false; o.Lockout.MaxFailedAccessAttempts = 5; o.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(15); }) .AddEntityFrameworkStores<GardenDbContext>() .AddDefaultTokenProviders(); ``` and its login handler passes `lockoutOnFailure: true`. An administrator can also lock a member explicitly with `SetLockoutEndDateAsync(user, DateTimeOffset.MaxValue)`, which only takes effect while `LockoutEnabled` is set. ## Inspecting and repairing lockout state `UserManager` exposes the state the sign-in path reads, which is what a support page or an admin tool needs: - `GetAccessFailedCountAsync(user)` — failures counted since the last success or lockout. - `GetLockoutEndDateAsync(user)` and `IsLockedOutAsync(user)` — when the lockout ends, and whether it is in force now. - `GetLockoutEnabledAsync(user)` and `SetLockoutEnabledAsync(user, true)` — the per-user switch that `AllowedForNewUsers` sets only at creation. - `SetLockoutEndDateAsync(user, null)` and `ResetAccessFailedCountAsync(user)` — unlock early and clear the count. `SetLockoutEndDateAsync` returns a failed `IdentityResult` with the code `UserLockoutNotEnabled` when the user's flag is off, which is the quickest way to spot accounts that were never eligible for lockout.
- Does a correct password unlock a locked-out account early?No. The pre-sign-in check returns `LockedOut` while `LockoutEnd` is in the future, before the password is verified, so even the right password is refused until the lockout expires or an administrator clears it with `SetLockoutEndDateAsync(user, null)`.
- Why might a user created by a seeding script never lock out?`AllowedForNewUsers` sets `LockoutEnabled` when `CreateAsync` runs. A seeding script that inserted rows directly, or ran with the option off, leaves `LockoutEnabled = false`, and `IsLockedOutAsync` then always returns `false` regardless of the failure count.
- Setting MaxFailedAccessAttempts is enough to turn lockout on everywhere.
- Identity's default minimum password length is 8 characters.
- A locked-out user who enters the correct password is let in immediately.
- UserManager.CheckPasswordAsync counts failed attempts toward lockout.
- AllowedForNewUsers retroactively enables lockout on existing accounts.