Permalink
Fetching contributors…
Cannot retrieve contributors at this time
252 lines (140 sloc) 13 KB
title author description ms.author ms.date uid
Introduction to Identity on ASP.NET Core
rick-anderson
Use Identity with an ASP.NET Core app. Learn how to set password requirements (RequireDigit, RequiredLength, RequiredUniqueChars, and more).
riande
08/08/2018
security/authentication/identity

Introduction to Identity on ASP.NET Core

By Rick Anderson

ASP.NET Core Identity is a membership system that adds login functionality to ASP.NET Core apps. Users can create an account with the login information stored in Identity or they can use an external login provider. Supported external login providers include Facebook, Google, Microsoft Account, and Twitter.

Identity can be configured using a SQL Server database to store user names, passwords, and profile data. Alternatively, another persistent store can be used, for example, Azure Table Storage.

View or download the sample code (how to download)).

In this topic, you learn how to use Identity to register, log in, and log out a user. For more detailed instructions about creating apps that use Identity, see the Next Steps section at the end of this article.

::: moniker range=">= aspnetcore-2.1"

AddDefaultIdentity and AddIdentity

AddDefaultIdentity was introduced in ASP.Core 2.1. Calling AddDefaultIdentity is similar to calling the following:

See AddDefaultIdentity source for more information.

::: moniker-end

Create a Web app with authentication

Create an ASP.NET Core Web Application project with Individual User Accounts.

Visual Studio

  • Select File > New > Project.
  • Select ASP.NET Core Web Application. Name the project WebApp1 to have the same namespace as the project download. Click OK.
  • Select an ASP.NET Core Web Application for ASP.NET Core 2.1, then select Change Authentication.
  • Select Individual User Accounts and click OK.

.NET Core CLI

dotnet new webapp --auth Individual -o WebApp1

The generated project provides ASP.NET Core Identity as a Razor Class Library.

Test Register and Login

Run the app and register a user. Depending on your screen size, you might need to select the navigation toggle button to see the Register and Login links.

toggle navbar button

[!INCLUDE]

Configure Identity services

Services are added in ConfigureServices. The typical pattern is to call all the Add{Service} methods, and then call all the services.Configure{Service} methods. The following code doesn't include the template generated CookiePolicyOptions:

::: moniker range=">= aspnetcore-2.1"

[!code-csharp]

The preceding code configures Identity with default option values. Services are made available to the app through dependency injection.

Identity is enabled by calling UseAuthentication. UseAuthentication adds authentication middleware to the request pipeline.

[!code-csharp]

::: moniker-end

::: moniker range="<= aspnetcore-2.0"

[!code-csharp]

Services are made available to the application through dependency injection.

Identity is enabled for the application by calling UseAuthentication in the Configure method. UseAuthentication adds authentication middleware to the request pipeline.

[!code-csharp]

::: moniker-end

::: moniker range="= aspnetcore-1.1"

[!code-csharp]

These services are made available to the application through dependency injection.

Identity is enabled for the application by calling UseIdentity in the Configure method. UseIdentity adds cookie-based authentication middleware to the request pipeline.

[!code-csharp]

::: moniker-end

For more information, see the IdentityOptions Class and Application Startup.

Scaffold Register, Login, and LogOut

Follow the Scaffold identity into a Razor project with authorization instructions to generate the the code shown in this section.

Visual Studio

Add the Register, Login, and LogOut files.

.NET Core CLI

If you created the project with name WebApp1, run the following commands. Otherwise, use the correct namespace for the ApplicationDbContext:

dotnet aspnet-codegenerator identity -dc WebApp1.Data.ApplicationDbContext --files "Account.Register;Account.Login;Account.Logout"

PowerShell uses semicolon as a command separator. When using PowerShell, escape the semicolons in the file list or put the file list in double quotes, as the preceding example shows.


Examine Register

::: moniker range=">= aspnetcore-2.1"

When a user clicks the Register link, the RegisterModel.OnPostAsync action is invoked. The user is created by CreateAsync on the _userManager object. _userManager is provided by dependency injection):

[!code-csharp]

::: moniker-end

::: moniker range="= aspnetcore-2.0"

When a user clicks the Register link, the Register action is invoked on AccountController. The Register action creates the user by calling CreateAsync on the _userManager object (provided to AccountController by dependency injection):

[!code-csharp]

::: moniker-end

If the user was created successfully, the user is logged in by the call to _signInManager.SignInAsync.

Note: See account confirmation for steps to prevent immediate login at registration.

Log in

::: moniker range=">= aspnetcore-2.1"

The Login form is displayed when:

  • The Log in link is selected.
  • A user attempts to access a restricted page that they aren't authorized to access or when they haven't been authenticated by the system.

When the form on the Login page is submitted, the OnPostAsync action is called. PasswordSignInAsync is called on the _signInManager object (provided by dependency injection).

[!code-csharp]

The base Controller class exposes a User property that you can access from controller methods. For instance, you can enumerate User.Claims and make authorization decisions. For more information, see xref:security/authorization/introduction.

::: moniker-end

::: moniker range="= aspnetcore-2.0"

The Login form is displayed when users select the Log in link or are redirected when accessing a page that requires authentication. When the user submits the form on the Login page, the AccountController Login action is called.

The Login action calls PasswordSignInAsync on the _signInManager object (provided to AccountController by dependency injection).

[!code-csharp]

The base (Controller or PageModel) class exposes a User property. For example, User.Claims can be enumerated to make authorization decisions.

::: moniker-end

Log out

::: moniker range=">= aspnetcore-2.1"

The Log out link invokes the LogoutModel.OnPost action.

[!code-csharp]

SignOutAsync clears the user's claims stored in a cookie. Don't redirect after calling SignOutAsync or the user will not be signed out.

Post is specified in the Pages/Shared/_LoginPartial.cshtml:

[!code-csharp]

::: moniker-end

::: moniker range="= aspnetcore-2.0"

Clicking the Log out link calls the LogOut action.

[!code-csharp]

The preceding code calls the _signInManager.SignOutAsync method. The SignOutAsync method clears the user's claims stored in a cookie.

::: moniker-end

Test Identity

The default web project templates allow anonymous access to the home pages. To test Identity, add [Authorize] to the About page.

[!code-csharp]

If you are signed in, sign out. Run the app and select the About link. You are redirected to the login page.

::: moniker range=">= aspnetcore-2.1"

Explore Identity

To explore Identity in more detail:

  • Create full identity UI source
  • Examine the source of each page and step through the debugger.

::: moniker-end

Identity Components

::: moniker range=">= aspnetcore-2.1"

All the Identity dependent NuGet packages are included in the Microsoft.AspNetCore.App metapackage.

::: moniker-end

The primary package for Identity is Microsoft.AspNetCore.Identity. This package contains the core set of interfaces for ASP.NET Core Identity, and is included by Microsoft.AspNetCore.Identity.EntityFrameworkCore.

Migrating to ASP.NET Core Identity

For more information and guidance on migrating your existing Identity store, see Migrate Authentication and Identity.

Setting password strength

See Configuration for a sample that sets the minimum password requirements.

Next Steps

  • Configure Identity
  • xref:security/authorization/secure-data
  • xref:security/authentication/add-user-data
  • xref:security/authentication/identity-enable-qrcodes
  • xref:migration/identity
  • xref:security/authentication/accconfirm
  • xref:security/authentication/2fa
  • xref:host-and-deploy/web-farm