AspNetCore.Docs/aspnetcore/security/authentication/scaffold-identity.md

6.0 KiB

title author description manager monikerRange ms.author ms.date ms.prod ms.technology ms.topic uid
Scaffold Identity in ASP.NET Core projects rick-anderson Learn how to scaffold Identity in an ASP.NET Core project. wpickett >= aspnetcore-2.1 riande 5/16/2018 asp.net-core aspnet article security/authentication/scaffold-identity

Scaffold Identity in ASP.NET Core projects

By Rick Anderson

ASP.NET Core 2.1 and later provides ASP.NET Core Identity as a Razor Class Library. Applications that include Identity can apply the scaffolder to selectively add the source code contained in the Identity Razor Class Library (RCL). You might want to generate source code so you can modify the code and change the behavior. For example, you could instruct the scaffolder to generate the code used in registration. Generated code takes precedence over the same code in the Identity RCL.

Applications that do not include authentication can apply the scaffolder to add the RCL Identity package. You have the option of selecting Identity code to be generated.

Although the scaffolder generates most of the necessary code, you'll have to update your project to complete the process. This document explains the steps needed to complete an Identity scaffolding update.

When the Identity scaffolder is run, a ScaffoldingReadme.txt file is created in the project directory. The ScaffoldingReadme.txt file contains general instructions on what's need to complete the Identity scaffolding update. This document contains more complete instructions than the read the ScaffoldingReadme.txt file.

We recommend using a source control system that shows file differences and allows you to back out of changes. Inspect the changes after running the Identity scaffolder.

Scaffold identity into an empty project

[!INCLUDE]

Add the following highlighted calls to the Startup class:

[!code-csharpMain]

[!INCLUDE]

[!INCLUDE]

Scaffold identity into a Razor project without existing authorization

[!INCLUDE]

Identity is configured in Areas/Identity/IdentityHostingStartup.cs. for more information, see IHostingStartup.

[!INCLUDE]

In the Configure method of the Startup class, call UseAuthentication after UseStaticFiles:

[!code-csharpMain]

[!INCLUDE]

Layout changes

Optional: Add the login partial (_LoginPartial) to the layout file:

[!code-htmlMain]

Scaffold identity into a Razor project with individual authorization

[!INCLUDE] Some Identity options are configured in Areas/Identity/IdentityHostingStartup.cs. For more information, see IHostingStartup.

Scaffold identity into an MVC project without existing authorization

[!INCLUDE]

Optional: Add the login partial (_LoginPartial) to the Views/Shared/_Layout.cshtml file:

[!code-htmlMain]

  • Move the Pages/Shared/_LoginPartial.cshtml file to Views/Shared/_LoginPartial.cshtml

Identity is configured in Areas/Identity/IdentityHostingStartup.cs. For more information, see IHostingStartup.

[!INCLUDE]

Call UseAuthentication after UseStaticFiles:

[!code-csharpMain]

[!INCLUDE]

Scaffold identity into an MVC project with individual authorization

[!INCLUDE]

Delete the Pages/Shared folder and the files in that folder.