Working With New ASP.Net Identity 2.0.0 in ASP.Net Application

Introduction

Microsoft has released the latest version for the ASP.NET Identity, ASP.NET Identity 2.0.0-alpha1. There are two new features in this version to the IdentityUser named "Email" and "IsConfirmed" as I described in the Introduction to ASP.NET Identity 2.0.0.

In that context, in this article you will see the migration process to 2.0.0-alpha1 from 1.0.0 version of ASP.NET Identity. We create this application with the ASP.NET Identity 1.0.0 version and create a new user registration for the application and next we'll make the change after upgrading the Identity to 2.0.0-aplha1.

Use the following procedure to create a sample.

Application Creation

Step 1: Open Visual Studio 2013 and create a new project.

Step 2: Select the Project template, either Web Forms or MVC as shown below: 

One ASP.NET in MVC

Step 3: Run the application and register the new user: 

Register in MVC

Now you'll see that the user registration is successful and back to the Home page. This step is analogous to users having used ASP.NET Identity 1.0.0 bits for user and role management.

Updating Identity

Step 4: Now just right-click on the project to open the Manager NuGet Packages.

Step 5: Go to the Updates section on the left pane and update the ASP.NET Identity packages.

Identity Update in NuGet Package

Step 6: After updating the packages, you can see the updated packages.config file: 

Packeges File in MVC

Step 7: Now run the application and login through the registered user and you would see the following Server Error in your application.

Model Backing Error in Identity

Enable Migration

Since the model has been changed you need to enable and add the migration. Use the following procedure to do that.

Step 8: Open the Package Manager Console and enter the following command:

Enable-Migrations 

Enable Migration in Package Manager Console

Step 9: Now enter the following command in the Package Manager Console:

add-migration update1 

Add Migration

It'll generate the following update1 class: 

namespace NewIdentityDemoApp.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    
public partial class update1 : DbMigration
    {
        public override void Up()
        {
            RenameColumn(table: "dbo.AspNetUserClaims", name: "User_Id", newName: "UserId");
            AddColumn("dbo.AspNetUsers", "Email", c => c.String());
            AddColumn("dbo.AspNetUsers", "IsConfirmed", c => c.Boolean(nullable: false));
            AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: false));
            DropColumn("dbo.AspNetUsers", "Discriminator");
        }
       
        public override void Down()
        {
            AddColumn("dbo.AspNetUsers", "Discriminator", c => c.String(nullable: false, maxLength: 128));
            AlterColumn("dbo.AspNetUsers", "UserName", c => c.String());
            DropColumn("dbo.AspNetUsers", "IsConfirmed");
            DropColumn("dbo.AspNetUsers", "Email");
            RenameColumn(table: "dbo.AspNetUserClaims", name: "UserId", newName: "User_Id");
        }
    }
}

 Step 10: Now enter the following command again in the console:

update-database -verbose

The verbose flag lets you view the SQL queries generated. Now the migration process is complete and there is no issue to login with the registered user in the application. 

Login with User

Summary

This article has introduced you to the new ASP.NET Identity version and also how to migrate to the new version from the old version of ASP.NET Identity. Thanks for reading.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now