--- title: Scaffold Identity in ASP.NET Core projects author: rick-anderson description: Learn how to scaffold Identity in an ASP.NET Core project. monikerRange: '>= aspnetcore-2.1' ms.author: riande ms.custom: mvc ms.date: 10/24/2018 uid: security/authentication/scaffold-identity --- # Scaffold Identity in ASP.NET Core projects By [Rick Anderson](https://twitter.com/RickAndMSFT) ASP.NET Core 2.1 and later provides [ASP.NET Core Identity](xref:security/authentication/identity) as a [Razor Class Library](xref:razor-pages/ui-class). 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. To gain full control of the UI and not use the default RCL, see the section [Create full identity UI source](#full). 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 needed to complete the Identity scaffolding update. This document contains more complete instructions than 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. > [!NOTE] > Services are required when using [Two Factor Authentication](xref:security/authentication/identity-enable-qrcodes), [Account confirmation and password recovery](xref:security/authentication/accconfirm), and other security features with Identity. Services or service stubs aren't generated when scaffolding Identity. Services to enable these features must be added manually. For example, see [Require Email Confirmation](xref:security/authentication/accconfirm#require-email-confirmation). ## Scaffold identity into an empty project [!INCLUDE[](~/includes/scaffold-identity/id-scaffold-dlg.md)] Add the following highlighted calls to the `Startup` class: [!code-csharp[](scaffold-identity/sample/StartupEmpty.cs?name=snippet1&highlight=5,20-23)] [!INCLUDE[](~/includes/scaffold-identity/hsts.md)] [!INCLUDE[](~/includes/scaffold-identity/migrations.md)] ## Scaffold identity into a Razor project without existing authorization [!INCLUDE[](~/includes/scaffold-identity/id-scaffold-dlg.md)] Identity is configured in *Areas/Identity/IdentityHostingStartup.cs*. for more information, see [IHostingStartup](xref:fundamentals/configuration/platform-specific-configuration). ### Migrations, UseAuthentication, and layout [!INCLUDE[](~/includes/scaffold-identity/migrations.md)] ### Enable authentication In the `Configure` method of the `Startup` class, call [UseAuthentication](/dotnet/api/microsoft.aspnetcore.builder.authappbuilderextensions.useauthentication?view=aspnetcore-2.0#Microsoft_AspNetCore_Builder_AuthAppBuilderExtensions_UseAuthentication_Microsoft_AspNetCore_Builder_IApplicationBuilder_) after `UseStaticFiles`: [!code-csharp[](scaffold-identity/sample/StartupRPnoAuth.cs?name=snippet1&highlight=29)] [!INCLUDE[](~/includes/scaffold-identity/hsts.md)] ### Layout changes Optional: Add the login partial (`_LoginPartial`) to the layout file: [!code-html[Main](scaffold-identity/sample/_Layout.cshtml?highlight=37)] ## Scaffold identity into a Razor project with authorization [!INCLUDE[](~/includes/scaffold-identity/id-scaffold-dlg-auth.md)] Some Identity options are configured in *Areas/Identity/IdentityHostingStartup.cs*. For more information, see [IHostingStartup](xref:fundamentals/configuration/platform-specific-configuration). ## Scaffold identity into an MVC project without existing authorization [!INCLUDE[](~/includes/scaffold-identity/id-scaffold-dlg.md)] Optional: Add the login partial (`_LoginPartial`) to the *Views/Shared/_Layout.cshtml* file: [!code-html[](scaffold-identity/sample/_LayoutMvc.cshtml?highlight=37)] * 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[](~/includes/scaffold-identity/migrations.md)] Call [UseAuthentication](/dotnet/api/microsoft.aspnetcore.builder.authappbuilderextensions.useauthentication?view=aspnetcore-2.0#Microsoft_AspNetCore_Builder_AuthAppBuilderExtensions_UseAuthentication_Microsoft_AspNetCore_Builder_IApplicationBuilder_) after `UseStaticFiles`: [!code-csharp[](scaffold-identity/sample/StartupMvcNoAuth.cs?name=snippet1&highlight=23)] [!INCLUDE[](~/includes/scaffold-identity/hsts.md)] ## Scaffold identity into an MVC project with authorization [!INCLUDE[](~/includes/scaffold-identity/id-scaffold-dlg-auth.md)] Delete the *Pages/Shared* folder and the files in that folder. ## Create full identity UI source To maintain full control of the Identity UI, run the Identity scaffolder and select **Override all files**. The following highlighted code shows the changes to replace the default Identity UI with Identity in an ASP.NET Core 2.1 web app. You might want to do this to have full control of the Identity UI. [!code-csharp[](scaffold-identity/sample/StartupFull.cs?name=snippet1&highlight=13-14,17-999)] The default Identity is replaced in the following code: [!code-csharp[](scaffold-identity/sample/StartupFull.cs?name=snippet2)] The following the code sets the [LoginPath](/dotnet/api/microsoft.aspnetcore.authentication.cookies.cookieauthenticationoptions.loginpath), [LogoutPath](/dotnet/api/microsoft.aspnetcore.authentication.cookies.cookieauthenticationoptions.logoutpath), and [AccessDeniedPath](/dotnet/api/microsoft.aspnetcore.authentication.cookies.cookieauthenticationoptions.accessdeniedpath): [!code-csharp[](scaffold-identity/sample/StartupFull.cs?name=snippet3)] Register an `IEmailSender` implementation, for example: [!code-csharp[](scaffold-identity/sample/StartupFull.cs?name=snippet4)] [!code-csharp[](scaffold-identity/sample/StartupFull.cs?name=snippet)] ## Additional resources * [Changes to authentication code to ASP.NET Core 2.1 and later](xref:migration/20_21#changes-to-authentication-code)