skip to content

In ASP.NET Core Identity, how do AddIdentity, AddDefaultIdentity and AddIdentityCore differ in what they register, and what do you chain after each?

level: middleimportance: must knowfreq 52%

answer

  1. cookies, UI and tokens: who adds which?
  2. AddDefaultIdentity lives in Identity.UI
  3. AddIdentityCore: UserManager only
  4. AddSignInManager, AddRoles, AddDefaultTokenProviders
  5. AddEntityFrameworkStores<TContext>

basics

~20 s

AddIdentity registers user and role managers, SignInManager and the Identity cookie schemes. AddDefaultIdentity adds the Razor Pages UI and token providers but no roles. AddIdentityCore registers only UserManager; you chain AddSignInManager, AddRoles and AddDefaultTokenProviders yourself.

solid answer

~30 s

`AddIdentity<TUser, TRole>` (in the shared framework) calls `AddAuthentication` with the Identity cookie schemes as defaults (`Identity.Application`, `Identity.External`, two two-factor cookies, `LoginPath` `/Account/Login`) and registers `UserManager`, `SignInManager` and `RoleManager`, but no token providers and no UI. `AddDefaultIdentity<TUser>` comes from the `Microsoft.AspNetCore.Identity.UI` package: it is `AddIdentityCore` plus `AddIdentityCookies`, `AddDefaultUI` (Razor Pages under `/Identity`, which also adds `SignInManager`) and `AddDefaultTokenProviders`, with no roles unless you chain `AddRoles<IdentityRole>()`. `AddIdentityCore<TUser>` registers only `UserManager` and its validators; no authentication, no `SignInManager`, no roles, no token providers. Every variant then needs a store, usually `AddEntityFrameworkStores<TContext>()`.

code

csharp · 6 lines
csharp
// API-only user store: Identity validation and hashing, no cookies
builder.Services.AddIdentityCore<GardenUser>(o => o.User.RequireUniqueEmail = true)
    .AddRoles<IdentityRole>()
    .AddEntityFrameworkStores<GardenDbContext>()
    .AddSignInManager()
    .AddDefaultTokenProviders();

go deeper

for a junior

Recall that the three methods register different amounts of Identity and that every one needs a store such as AddEntityFrameworkStores.

for a middle

List what each registers — cookies, SignInManager, roles, token providers, UI — and which chained calls fill the gaps.

for a senior

Diagnose the classic gaps: roles missing with AddDefaultIdentity, NotSupportedException for tokens with AddIdentity, no SignInManager with AddIdentityCore, duplicate schemes when two are combined.

for a principal

Choose a registration per app type — server-rendered pages, SPA plus API, Blazor — and keep it consistent across the solution's services.

## Why there are three entry points ASP.NET Core Identity is several pieces: user management, role management, sign-in, cookie schemes, token providers for e-mail and reset links, and an optional ready-made UI. The three registration methods bundle different subsets. | | `AddIdentity<TUser, TRole>` | `AddDefaultIdentity<TUser>` | `AddIdentityCore<TUser>` | |---|---|---|---| | Package | shared framework | `Microsoft.AspNetCore.Identity.UI` | shared framework | | `UserManager` | yes | yes | yes | | `RoleManager` | yes | no (chain `AddRoles`) | no (chain `AddRoles`) | | `SignInManager` | yes | yes (via `AddDefaultUI`) | no (chain `AddSignInManager`) | | Authentication and Identity cookies | yes, set as defaults | yes, via `AddIdentityCookies` | no | | Token providers | no (chain `AddDefaultTokenProviders`) | yes | no | | UI | none | Razor Pages under `/Identity` | none | | Login path | `/Account/Login` | `/Identity/Account/Login` | not applicable | ## `AddIdentity<TUser, TRole>` The classic full registration for an MVC or Razor Pages app with your own pages. It calls `AddAuthentication` with `DefaultAuthenticateScheme` and `DefaultChallengeScheme` set to `Identity.Application` and `DefaultSignInScheme` set to `Identity.External`, then adds four cookies: - `Identity.Application` — the signed-in user, with the security stamp validator on `OnValidatePrincipal`. - `Identity.External` — a five-minute cookie holding the result of an external login until it is linked. - `Identity.TwoFactorUserId` — a five-minute cookie between password and second factor. - `Identity.TwoFactorRememberMe` — remembers a browser that passed two-factor. It does **not** add token providers, so `GenerateEmailConfirmationTokenAsync` throws a `NotSupportedException` ("No IUserTwoFactorTokenProvider<TUser> named 'Default' is registered.") until you chain `AddDefaultTokenProviders()`. ## `AddDefaultIdentity<TUser>` What the MVC and Razor Pages templates with Individual accounts use, typically with `o.SignIn.RequireConfirmedAccount = true`. Internally it calls `AddIdentityCore` (setting `Stores.MaxLengthForKeys` to 128), `AddIdentityCookies`, `AddDefaultUI` and `AddDefaultTokenProviders`. The UI is a Razor Class Library of pages (login, register, manage account) you override by scaffolding. There is **no role support** until you chain `.AddRoles<IdentityRole>()`, a frequent surprise when `[Authorize(Roles = "...")]` never matches. ## `AddIdentityCore<TUser>` The minimal registration: `UserManager<TUser>`, user and password validators, the password hasher, the lookup normalizer and the claims principal factory. Nothing authenticates requests. You build up: 1. `.AddSignInManager()` if you sign users in through Identity. 2. `.AddRoles<TRole>()` for roles. 3. `.AddDefaultTokenProviders()` for confirmation and reset tokens. 4. `services.AddAuthentication(...).AddIdentityCookies()` if you want the Identity cookies. The Blazor Web App template with Individual accounts uses exactly this shape, adding `AddSignInManager()` and `AddDefaultTokenProviders()`. ## The store None of the three knows where users live. `AddEntityFrameworkStores<TContext>()` (package `Microsoft.AspNetCore.Identity.EntityFrameworkCore`) registers EF Core user stores and, when a role type is configured, a `RoleStore`, all over your `IdentityDbContext` subclass. Registering that `DbContext` is a separate call. ## For the garden booking site The site has its own Razor Pages for booking and wants lockout, e-mail confirmation and passkeys: ```csharp builder.Services.AddIdentity<GardenUser, IdentityRole>(o => { o.SignIn.RequireConfirmedAccount = true; o.Lockout.MaxFailedAccessAttempts = 5; }) .AddEntityFrameworkStores<GardenDbContext>() .AddDefaultTokenProviders(); ``` `AddIdentity` brings roles for plot coordinators and the cookies; `AddDefaultTokenProviders` makes confirmation links work. ## Choosing quickly - **Server-rendered app, default pages are fine:** `AddDefaultIdentity`, plus `AddRoles` if you need roles. - **Server-rendered app, your own pages:** `AddIdentity`, plus `AddDefaultTokenProviders`. - **Blazor Web App:** what the template generates — `AddIdentityCore` with `AddSignInManager`, `AddDefaultTokenProviders` and `AddIdentityCookies`. - **API that only needs user storage and password checks:** `AddIdentityCore`, adding pieces as needed. - **First-party SPA or mobile client with JSON endpoints:** `AddIdentityApiEndpoints`, a separate topic.

  • An app uses AddDefaultIdentity<IdentityUser>() and [Authorize(Roles = "Coordinator")] never succeeds. Why?
    `AddDefaultIdentity` builds on `AddIdentityCore`, which registers no role type, so no `RoleManager` exists and the claims factory adds no role claims. Chain `.AddRoles<IdentityRole>()` before `AddEntityFrameworkStores` so roles are stored and emitted into the cookie's principal.
  • Can you call AddIdentity and AddDefaultIdentity together?
    It is a mistake: both register the `Identity.Application` cookie scheme, and adding a scheme name twice makes `AuthenticationOptions` throw an `InvalidOperationException` ("Scheme already exists: Identity.Application") when the options are built. Pick one; if you want the default UI pages with `AddIdentity`, chain `.AddDefaultUI()` on it instead.

  • AddIdentityCore registers SignInManager and the Identity cookies too.
  • AddDefaultIdentity includes role support out of the box.
  • AddIdentity adds the e-mail confirmation and reset token providers automatically.
  • AddEntityFrameworkStores registers the DbContext, so AddDbContext is unnecessary.
  • AddDefaultIdentity is part of the shared framework, so no package is needed.