liveBook · Manning

Chapter 23: Authentication: Adding users to your application with Identity

What you will learn in chapter 23 of ASP.NET Core in Action, 3e

This chapter introduces authentication and ASP.NET Core Identity, previewing how to add, explore, customize, and extend user accounts in web applications.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces authentication and ASP.NET Core Identity, previewing how to add, explore, customize, and extend user accounts in web applications.

Key terms: authentication, authorization, current logged-in user, ASP.NET Core Identity, Identity, Entity Framework Core, EF Core, passwords, default Identity UI, scaffolding

23.1 Introducing authentication and authorization

This section distinguishes authentication from authorization and introduces how ASP.NET Core identifies a signed-in user across requests.

Key terms: authentication, authorization, sign in, traditional web app, subsequent requests, currently signed-in user

23.1.1 Understanding users and claims in ASP.NET Core

This section explains how ASP.NET Core represents the current request’s user as a claims-based principal stored in HttpContext.User.

Key terms: HttpContext, HttpContext.User, principal, ClaimsPrincipal, claim, claim type, claim value, HasAdminAccess, IsVipCustomer, Kestrel

23.1.2 Authentication in ASP.NET Core: Services and middleware

This section traces cookie-based sign-in and subsequent-request authentication in ASP.NET Core, including the roles of services, middleware, and external identity providers.

Key terms: SignInManager, ClaimsPrincipal, HttpContext.User, authentication cookie, model binding, AuthenticationMiddleware, authentication services, AuthorizationMiddleware, identity provider, OpenID Connect

23.2 What is ASP.NET Core Identity?

This section explains what ASP.NET Core Identity provides for storing and managing application users, where its boundaries are, and why it is usually safer than building user management yourself.

Key terms: ASP.NET Core Identity, claims, strong hashing algorithm, MFA, brute-force attacks, account lockout, EF Core, stores, user details, database integration

23.3 Creating a project that uses ASP.NET Core Identity

This section introduces examining a template-generated ASP.NET Core project to see Identity working in a real application and locate its integration points.

Key terms: ASP.NET Core Identity, ASP.NET Core templates, Identity

23.3.1 Creating the project from a template

This section creates a Razor Pages application with individual-account Identity from a template and prepares its database so users can be registered.

Key terms: ASP.NET Core Web Application, Razor Pages, Authentication Type, Individual Accounts, dotnet restore, LocalDB, EF Core migrations, SQL Server, dotnet ef database update, database provider

23.3.2 Exploring the template in Solution Explorer

This section tours the Identity-enabled project template, showing its default UI structure, service registration, and authentication middleware placement.

Key terms: Areas, Identity area, default UI, ApplicationDbContext, migrations, _LoginPartial.cshtml, Microsoft.AspNetCore.Identity.UI, Areas/Identity/Pages, _ViewStart.cshtml, _Layout.cshtml

23.3.3 The ASP.NET Core Identity data model

Explains how ASP.NET Core Identity stores users and related authentication data with EF Core, and how database users become authenticated claims principals.

Key terms: IdentityDbContext, IdentityUser, ApplicationDbContext, EF Core, migrations, connection string, dotnet ef, project folder, AspNetUsers, AspNetUserClaims

23.3.4 Interacting with ASP.NET Core Identity

This section tours ASP.NET Core Identity’s default UI, its built-in account flows, and the common customizations an application needs.

Key terms: default UI, IdentityUser, registration page, email confirmation, Login page, Identity UI, MFA, authenticator app, management pages, email-sending service

23.4 Adding ASP.NET Core Identity to an existing project

This section introduces the plan and intended result for adding default ASP.NET Core Identity user accounts to an existing EF Core Razor Pages recipe application.

Key terms: ASP.NET Core Identity, NuGet packages, dependency injection (DI) container, EF Core data model, Identity entities, Identity UI, registration page, login screen, manage account screen, login status

23.4.1 Configuring the ASP.NET Core Identity services

This section shows how to configure an existing EF Core application to use ASP.NET Core Identity and its default UI.

Key terms: Microsoft.AspNetCore.Identity.EntityFrameworkCore, Microsoft.AspNetCore.Identity.UI, default UI, dotnet restore, Program.cs, ASP.NET Core Identity services, AppDbContext, ApplicationUser, EF Core, AuthenticationMiddleware

23.4.2 Updating the EF Core data model to support Identity

This section adapts an existing EF Core data model for ASP.NET Core Identity by introducing an application user type, inheriting from IdentityDbContext, and migrating the added schema to the database.

Key terms: ApplicationUser, IdentityUser, DbContext, IdentityDbContext, IdentityDbContext<ApplicationUser>, DbSet<T>, EF Core data model, migration, AddIdentitySchema, database

23.4.3 Updating the Razor views to link to the Identity UI

This subsection links an existing application’s Razor layout and login widget to ASP.NET Core Identity’s default UI.

Key terms: _ViewStart.cshtml, Areas/Identity/Pages, Layout.cshtml, Identity Razor Pages, default UI, _LoginPartial.cshtml, Pages/Shared, _Layout.cshtml, ApplicationUser, IdentityUser

23.5 Customizing a page in ASP.NET Core Identity’s default UI

This section shows how to scaffold and selectively override an ASP.NET Core Identity default UI page while retaining as much of the built-in implementation as possible.

Key terms: scaffolding, default Identity UI, Razor Pages, override, Microsoft.VisualStudio.Web.CodeGeneration.Design, Microsoft.EntityFrameworkCore.Tools, Identity scaffolder, Account/Register, AppDbContext, Areas/Identity/Pages/Account

23.6 Managing users: Adding custom data to users

This section shows how to add custom claims to Identity users during registration and read those claims from the current user in Razor Pages and templates.

Key terms: Claim, ApplicationUser, UserManager<ApplicationUser>, CreateAsync, AddClaimAsync, Register.cshtml, ClaimsPrincipal, HttpContext.User, SignInAsync, Type

23.7 Summary

This summary recaps how ASP.NET Core identifies users with claims-based authentication and how ASP.NET Core Identity supplies database-backed user management and login support.

Where this is used later

Concepts explained in this chapter

← Chapter 22: Creating custom MVC and Razor Page filters Chapter 24: Authorization: Securing your application →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.