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 the 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 the user and role management.
 

Updating Identity

 
Step 4: Now just right-click on the project to open the Manage 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:
  1. Enable-Migrations  
Enable Migration in Package Manager Console
 
Step 9: Now enter the following command in the Package Manager Console:
  1. add-migration update1  
Add Migration
 
It'll generate the following update1 class: 
  1. namespace NewIdentityDemoApp.Migrations  
  2. {  
  3.     using System;  
  4.     using System.Data.Entity.Migrations;  
  5.   
  6.     public partial class update1 : DbMigration  
  7.     {  
  8.         public override void Up()  
  9.         {  
  10.             RenameColumn(table: "dbo.AspNetUserClaims", name: "User_Id", newName: "UserId");  
  11.             AddColumn("dbo.AspNetUsers""Email", c => c.String());  
  12.             AddColumn("dbo.AspNetUsers""IsConfirmed", c => c.Boolean(nullable: false));  
  13.             AlterColumn("dbo.AspNetUsers""UserName", c => c.String(nullable: false));  
  14.             DropColumn("dbo.AspNetUsers""Discriminator");  
  15.         }  
  16.          
  17.         public override void Down()  
  18.         {  
  19.             AddColumn("dbo.AspNetUsers""Discriminator", c => c.String(nullable: false, maxLength: 128));  
  20.             AlterColumn("dbo.AspNetUsers""UserName", c => c.String());  
  21.             DropColumn("dbo.AspNetUsers""IsConfirmed");  
  22.             DropColumn("dbo.AspNetUsers""Email");  
  23.             RenameColumn(table: "dbo.AspNetUserClaims", name: "UserId", newName: "User_Id");  
  24.         }  
  25.     }  

Step 10: Now enter the following command again in the console:
  1. 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.


Similar Articles