ASP.NET Core Identity lifecycle
ASP.NET Core Identity lifecycle
ASP.NET Core Identity manages application accounts and their lifecycle: user storage, password verification, role membership, sign-in, tokens for recovery and security-stamp validation. It is not automatically a general-purpose OAuth or OpenID Connect authorization server. Separate account storage, sign-in/session policy and resource permissions when designing an app.
Identity uses UserManager to manage users and their credentials, RoleManager for role stores, and SignInManager for sign-in and session coordination. Use maintained password hashing rather than raw SHA-256 or reversible encryption. PasswordHasher<TUser> produces a salted, versioned hash; verification may return Failed, Success or SuccessRehashNeeded. When a rehash is required, use the normal Identity update flow rather than inventing a hash migration. Configure lockout, password policy, confirmed contact requirements, MFA and recovery according to a threat model. Recovery should avoid account enumeration, use expiring purpose-bound tokens, rate limit requests, verify ownership and invalidate relevant sessions as appropriate. Do not send passwords or recovery tokens to logs. Security stamps let the framework revalidate a cookie against account changes at configured intervals; revocation is not necessarily immediate. Logout removes a client's cookie but does not guarantee every previously issued session is invalidated. For APIs, Identity's built-in bearer tokens are proprietary, not JWTs; use a suitable identity provider for interoperable OAuth/OIDC. The examples below demonstrate real password hashing and option configuration, not a persistent account store or real recovery delivery.
Baseline: .NET 10 / C# 14 with Microsoft.AspNetCore.App shared framework. All snippets are independent console fixtures, not ByteMind runner code. In an approved isolated environment with a reviewed installed SDK and targeting packs, use dotnet build and dotnet run --no-build in the fixture directory; restore only from approved sources if required. No external service, real user data, account or credential is needed. Expected results are reasoned, not executed; cryptographic random data is not printed.
Verify a password through the maintained hasher
Use a fictional user and invented test string; do not copy credentials from your machine.
using Microsoft.AspNetCore.Identity;var user = new IdentityUser("fixture-reader");var hasher = new PasswordHasher<IdentityUser>();var hash = hasher.HashPassword(user, "fixture-only-test-string");Console.WriteLine(hasher.VerifyHashedPassword(user, hash, "fixture-only-test-string") != PasswordVerificationResult.Failed);Console.WriteLine(hasher.VerifyHashedPassword(user, hash, "different-fixture-string") == PasswordVerificationResult.Failed);Expected output (not executed):
TrueTrueCorrect input verifies; different input fails. The random salted hash is never displayed. Real accounts should use UserManager and configured policy.
Check salted hashes without printing them
Hash the same synthetic password twice. The output is a comparison, not secret material.
using Microsoft.AspNetCore.Identity;var user = new IdentityUser("fixture-reader");var hasher = new PasswordHasher<IdentityUser>();var first = hasher.HashPassword(user, "fixture-only-test-string");var second = hasher.HashPassword(user, "fixture-only-test-string");Console.WriteLine(first != second);Console.WriteLine(hasher.VerifyHashedPassword(user, first, "fixture-only-test-string") != PasswordVerificationResult.Failed);Console.WriteLine(hasher.VerifyHashedPassword(user, second, "fixture-only-test-string") != PasswordVerificationResult.Failed);Expected output (not executed):
TrueTrueTrueIndependent random salts are expected to produce different hashes (collision is negligible, not impossible); both verify against the same input.
Inspect Identity lifecycle policy options
Configure illustrative account requirements without creating a store or an account. Values are examples, not universal policy recommendations.
using Microsoft.AspNetCore.Identity;using Microsoft.Extensions.DependencyInjection;using Microsoft.Extensions.Options;var services = new ServiceCollection();services.AddIdentityCore<IdentityUser>(o => { o.SignIn.RequireConfirmedAccount = true; o.Lockout.AllowedForNewUsers = true; o.Lockout.MaxFailedAccessAttempts = 5;});services.Configure<SecurityStampValidatorOptions>(o => o.ValidationInterval = TimeSpan.FromMinutes(10));using var provider = services.BuildServiceProvider();var identity = provider.GetRequiredService<IOptions<IdentityOptions>>().Value;var stamps = provider.GetRequiredService<IOptions<SecurityStampValidatorOptions>>().Value;Console.WriteLine(identity.SignIn.RequireConfirmedAccount);Console.WriteLine(identity.Lockout.MaxFailedAccessAttempts);Console.WriteLine(stamps.ValidationInterval.TotalMinutes == 10);Expected output (not executed):
True5TrueThese settings are observable configuration, not proof of enforcement. Actual sign-in, store support, cookie validation and lockout paths require end-to-end testing.
Apply the concept
Design a recovery flow for a fictional account whose password was forgotten. Describe messages to known and unknown emails, token handling, MFA recovery, session revocation and tests. Do not implement delivery.
Use the manual practice and knowledge check to reason about the boundary. Do not deploy these fixtures as production security controls.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
ASP.NET Core Identity lifecycle — knowledge check
Apply the lesson concepts to the following scenarios.
Which component should coordinate ordinary password changes?
What does SuccessRehashNeeded indicate?
Why avoid distinct recovery messages for known versus unknown accounts?
Are the built-in Identity API bearer tokens JWTs?
Does changing the security stamp instantly invalidate every cookie on every server?