ASP.NET Core Identity
ASP.NET Core Identity is the built-in system for user accounts: registration, sign-in, password hashing, lockout, two-factor authentication and external logins. This lesson shows what Identity gives you, how it stores users with EF Core and how sign-in works.
- StartRegisterUserManager.CreateAsync hashes the password and saves the user
- Step 2Stored with EF CoreTables such as AspNetUsers and AspNetRoles
- Step 3Sign inSignInManager.PasswordSignInAsync checks the password hash
- Step 4Cookie issuedAn encrypted authentication cookie holds the user's claims
- Step 5Later requestsAuthentication middleware reads the cookie and sets User
- EndIn your codeUser.Identity.IsAuthenticated, User.Identity.Name, User.IsInRole
The idea in plain words
Writing your own login system is risky: password hashing, lockout after failed attempts, email confirmation and two-factor authentication are easy to get wrong. Identity does all of that. It stores users, roles and claims through EF Core, and signs users in with an encrypted cookie.
The quickest start is the template option dotnet new mvc --auth Individual, which wires up Identity with ready-made register, login and manage pages from the Identity UI package. You can scaffold those pages into your project to customise them.
Example: setup
// ApplicationDbContext.cs
public class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
}
// Program.cs
builder.Services.AddDbContext<ApplicationDbContext>(o =>
o.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
builder.Services.AddDefaultIdentity<IdentityUser>(o => o.SignIn.RequireConfirmedAccount = true)
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
var app = builder.Build();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages(); // the Identity UI pagesExample: signing in from your own controller
[HttpPost, ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginModel model, string? returnUrl = null)
{
if (!ModelState.IsValid) return View(model);
var result = await _signInManager.PasswordSignInAsync(
model.Email, model.Password, model.RememberMe, lockoutOnFailure: true);
if (result.Succeeded) return LocalRedirect(returnUrl ?? "/");
if (result.IsLockedOut) return View("Lockout");
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}In Visual Studio 2026
The easiest start is when you create the project: on the Additional information screen set Authentication type to Individual Accounts. To add Identity to an existing app, right-click the project, choose Add, New Scaffolded Item, then Identity, and pick the pages you want to customise. Afterwards run Update-Database in the Package Manager Console to create the Identity tables.
Common mistakes
- Storing passwords yourself, even hashed with a simple algorithm. Let Identity's password hasher do it.
- Calling UseAuthorization before UseAuthentication.
- Redirecting to returnUrl without LocalRedirect, which allows open redirects to other sites.
- Turning off lockout and email confirmation for convenience in production.
Interview question
What is the difference between authentication and authorization?
Authentication answers who the user is: verifying credentials and establishing an identity, for example Identity checking a password and issuing a cookie that the authentication middleware reads on later requests. Authorization answers what the user may do: checking that identity against rules such as [Authorize], roles or policies. Authentication always happens first.
Want the full details? Read the official Microsoft documentation for this topic.