liveBook · Manning

ASP.NET Core Identity

As explained in Pro ASP.NET Core 7, Tenth Edition

ASP.NET Core Identity is an extensible membership system for managing users, credentials, claims, roles, and related authentication functionality in ASP.NET Core applications.

ASP.NET Core Identity manages application users and connects authentication and authorization to the ASP.NET Core request pipeline. Use it when users must establish who they are before reaching restricted features. It is a toolkit rather than a ready-made security policy: choose the account-management and access rules the application needs. section 38.0 section 11.0

A typical database-backed setup has four parts:

  • Install Microsoft.AspNetCore.Identity.EntityFrameworkCore, create an IdentityDbContext<IdentityUser>, configure its connection string, and prepare the database with a migration. IdentityUser supplies core user fields; a custom user class is also possible. section 38.2 section 38.2.1 section 38.0
  • Register the context and call AddIdentity with user and role types. Add authentication and authorization middleware to the request pipeline. section 11.1.4
  • Use UserManager<T> to find, create, update, and delete users. Use RoleManager<T> to manage roles and UserManager<T> to assign users to them. Configure username and password rules through options. section 38.3 section 38.0
  • Use SignInManager<T> to validate credentials, then apply authorization to endpoints or Blazor components with the Authorize attribute. Web-service clients can use cookies or bearer tokens. section 39.0

After sign-in, authentication middleware reads the cookie and sets HttpContext.User to a ClaimsPrincipal. An endpoint can check whether the request is authenticated and obtain the username without handling the cookie itself; UserManager<T> can then retrieve the corresponding user. section 39.2.5

Protecting an administration area

Give an Admin account access to administration features while keeping customers out. The account needs stored Identity data, a way to sign in, authentication middleware to associate the user with each request, and authorization on the restricted features. This is a basic setup, not an implementation of Identity’s many other integration options. section 11.0 section 38.0 section 39.0

Table 39.1 Chapter guide (view table figure)

Problem

Solution

Listing

Authenticating users

Use the SignInManager<T> class to validate the credentials users provide and use the built-in middleware to trigger authentication.

3–8

Restricting access to endpoints

Use the Authorize attribute and the built-in middleware to control access.

9–13

Restricting access to Blazor components

Use the Authorize attribute and the built-in Razor Components to control access.

14–17

Restricting access to web services

Use cookie authentication or bearer tokens.

18–30

The guide maps sign-in, endpoint and Blazor authorization, and web-service authentication to the Identity components used for each task. section 39.0

How Pro ASP.NET Core 7, Tenth Edition covers it

How deep it goes
Works through database setup, account and role management, sign-in, and authorization; leaves Identity’s wider integration options unexplored.
Examples
Uses a protected administration area, an Identity database context, account-management tools, and an authenticated-user details page.
What it assumes
Assumes ASP.NET Core project setup; supplies Identity configuration steps and permits treating unfamiliar Entity Framework Core context code as a black box.
What you can do after
Build it from scratch: configure database-backed Identity, manage accounts and roles, authenticate users, and restrict access.
How it connects
Links Entity Framework Core, middleware, claims, roles, authorization attributes, Blazor, and bearer tokens.

Also explained in

Compare how 4 books explain ASP.NET Core Identity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pro ASP.NET Core 7, Tenth Edition. The section links point to the parts of the book it draws on.