liveBook · Manning

Identity Razor Pages

As explained in ASP.NET Core in Action, 3e

ASP.NET Core Identity UI is the built-in Razor Pages library that provides standard user-account and authentication interfaces for ASP.NET Core Identity.

Identity Razor Pages are the ready-made account screens supplied by Microsoft.AspNetCore.Identity.UI. They turn ASP.NET Core Identity’s user-management services into a usable registration and sign-in flow without requiring you to write every page yourself. The default UI contains more than 30 Razor Pages, including pages for login, registration, account management, two-factor authentication, and external login providers. Start with these pages, then replace only the ones your application needs to change. section 23.2 section 23.5

The pages live in the Identity area. A project can use the packaged pages without showing their files in its own Pages folder. To override one, put a corresponding page under Areas/Identity/Pages. A _ViewStart.cshtml file in that folder can point the Identity pages at the application’s shared layout, so account screens look like the rest of the site. section 23.3.2 section 23.4.3

For an existing Razor Pages application that uses EF Core, connect the UI in manageable steps:

  • Reference Microsoft.AspNetCore.Identity.EntityFrameworkCore and Microsoft.AspNetCore.Identity.UI, then restore the packages. section 23.4.1
  • Register Identity with AddDefaultIdentity<TUser>() and connect it to the existing DbContext with AddEntityFrameworkStores<TContext>(). section 23.4.1
  • Derive the DbContext from IdentityDbContext<TUser>, create a migration, and apply the database changes needed for Identity. section 23.4.2
  • Set the Identity pages’ layout and add a login partial to the application layout for registration, sign-in, and signed-in account links. section 23.4.3

What a new user sees

A visitor opens Register, enters an email and password, and is sent to a confirmation screen. They can then use Login and, once signed in, reach pages for changing their email or password, configuring an authenticator app, or deleting personal data. Configure an email-sending service before relying on email confirmation: none is enabled by default. External providers also need configuration, and MFA QR-code generation is not supplied for you. section 23.3.4 section 23.4.3

Customize a page when its wording or appearance does not suit your users. For example, scaffold Register.cshtml and remove its developer-facing explanation of external login providers. Visual Studio or the .NET CLI can generate the page in the override location, with its Razor template and PageModel ready to edit. Leave the other packaged pages in place; scaffolding the whole UI creates more code to maintain. section 23.5

How ASP.NET Core in Action, 3e covers it

How deep it goes
Works through existing-app setup, page links, and single-page scaffolding, while leaving email delivery and some MFA integration to separate configuration.
Examples
Uses a registration-to-login flow, an existing EF Core application, a login partial, and a scaffolded registration page.
What it assumes
Assumes an existing Razor Pages and EF Core application for the integration walkthrough; introduces the Identity area and override location.
What you can do after
Operate/configure it in an existing Razor Pages app and replace an individual account page without rebuilding the default UI.
How it connects
Connects Razor Pages and Areas with ASP.NET Core Identity, EF Core, authentication middleware, and scaffolding.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.