--- title: Migrating Authentication and Identity author: ardalis description: keywords: ASP.NET Core, ms.author: riande manager: wpickett ms.date: 10/14/2016 ms.topic: article ms.assetid: 0db145cb-41a5-448a-b889-72e2d789ad7f ms.technology: aspnet ms.prod: asp.net-core uid: migration/identity --- # Migrating Authentication and Identity By [Steve Smith](https://ardalis.com/) In the previous article we [migrated configuration from an ASP.NET MVC project to ASP.NET Core MVC](configuration.md). In this article, we migrate the registration, login, and user management features. ## Configure Identity and Membership In ASP.NET MVC, authentication and identity features are configured using ASP.NET Identity in Startup.Auth.cs and IdentityConfig.cs, located in the App_Start folder. In ASP.NET Core MVC, these features are configured in *Startup.cs*. Install the `Microsoft.AspNetCore.Identity.EntityFrameworkCore` and `Microsoft.AspNetCore.Authentication.Cookies` NuGet packages. Then, open Startup.cs and update the `ConfigureServices()` method to use Entity Framework and Identity services: ```csharp public void ConfigureServices(IServiceCollection services) { // Add EF services to the services container. services.AddEntityFramework(Configuration) .AddSqlServer() .AddDbContext(); // Add Identity services to the services container. services.AddIdentity(Configuration) .AddEntityFrameworkStores(); services.AddMvc(); } ``` At this point, there are two types referenced in the above code that we haven't yet migrated from the ASP.NET MVC project: `ApplicationDbContext` and `ApplicationUser`. Create a new *Models* folder in the ASP.NET Core project, and add two classes to it corresponding to these types. You will find the ASP.NET MVC versions of these classes in `/Models/IdentityModels.cs`, but we will use one file per class in the migrated project since that's more clear. ApplicationUser.cs: ```csharp using Microsoft.AspNetCore.Identity.EntityFrameworkCore; namespace NewMvc6Project.Models { public class ApplicationUser : IdentityUser { } } ``` ApplicationDbContext.cs: ```csharp using Microsoft.AspNetCore.Identity.EntityFramework; using Microsoft.Data.Entity; namespace NewMvc6Project.Models { public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext() { Database.EnsureCreated(); } protected override void OnConfiguring(DbContextOptionsBuilder options) { options.UseSqlServer(); } } } ``` The ASP.NET Core MVC Starter Web project doesn't include much customization of users, or the ApplicationDbContext. When migrating a real application, you will also need to migrate all of the custom properties and methods of your application's user and DbContext classes, as well as any other Model classes your application utilizes (for example, if your DbContext has a DbSet, you will of course need to migrate the Album class). With these files in place, the Startup.cs file can be made to compile by updating its using statements: ```csharp using Microsoft.Framework.ConfigurationModel; using Microsoft.AspNetCore.Hosting; using NewMvc6Project.Models; using Microsoft.AspNetCore.Identity; ``` Our application is now ready to support authentication and identity services - it just needs to have these features exposed to users. ## Migrate Registration and Login Logic With identity services configured for the application and data access configured using Entity Framework and SQL Server, we are now ready to add support for registration and login to the application. Recall that [earlier in the migration process](mvc.md#migrate-layout-file) we commented out a reference to _LoginPartial in _Layout.cshtml. Now it's time to return to that code, uncomment it, and add in the necessary controllers and views to support login functionality. Update _Layout.cshtml; uncomment the @Html.Partial line: ```cshtml
  • @Html.ActionLink("Contact", "Contact", "Home")
  • @*@Html.Partial("_LoginPartial")*@ ``` Now, add a new MVC View Page called _LoginPartial to the Views/Shared folder: Update _LoginPartial.cshtml with the following code (replace all of its contents): ```cshtml @inject SignInManager SignInManager @inject UserManager UserManager @if (SignInManager.IsSignedIn(User)) { } else { } ``` At this point, you should be able to refresh the site in your browser. ## Summary ASP.NET Core introduces changes to the ASP.NET Identity features. In this article, you have seen how to migrate the authentication and user management features of an ASP.NET Identity to ASP.NET Core.