Updating ASP.NET applications from ASP.NET Identity 1.0 to 2.0.0-alpha1

Updating ASP.NET applications from ASP.NET Identity 1.0 to 2.0.0-alpha1

Rate This
  • Comments 20

We released the 2.0.0-alpha1 version for ASP.NET Identity. Learn more here http://blogs.msdn.com/b/webdev/archive/2013/12/20/announcing-preview-of-microsoft-aspnet-identity-2-0-0-alpha1.aspx

One of the features in this version is Account Confirmation and Password Reset. As part of this new feature we have added two new properties to the IdentityUser class namely ‘Email’ and ‘IsConfirmed’. This results in a change of the schema created by the ASP.NET Identity system in 2.0. Updating the package in an existing application (which was using 1.0) will cause the application to fail since the Entity Framework model has been changed. You need to update your database schema to use the 2.0 features. This can be done using Entity Framework migrations. More information on migrations can be found here.

This article provides a quick overview on the migration process to 2.0.0-alpha1 from 1.0.0 version of ASP.NET Identity. We start with a web application with 1.0.0. version of Identity, have a local user registered for the application, update the Identity bits to 2.0.0-alpha1 and then migrate the database to make the application run as expected.

1. On Visual Studio 2013 RTM create a new 4.5 ASP.NET Web application(you can choose MVC or Web Forms)

clip_image001

 

2. Run the application and create a new local user. This step is analogous to users having used ASP.NET Identity 1.0.0 bits for user and role management and the database being created to store these information at the backend.

3. Now right click on project and select ‘Manage Nuget Packages’. Go to the ‘Update’ section and update the Identity packages

clip_image003

4. Now run the application and try to login old user. You would see the below error which shows that migration is needed on the database since the model is changed.

clip_image005

5. To enable migrations in Visual Studio, click on ‘View’, select ‘Package Manager Console’.

a. Enter ‘Enable-Migrations’. This is create the initial configuration for the database

b. Next enter ‘Add-Migration Update1’. Here ‘Update1’ is an identifier you can have for this migration. The migrations code generated should as below

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");

}

To summarize the changes, there are 2 new columns added to the AspNetUsers table namely ‘Email’ and ‘IsUserConfirmed’. Also the ‘User_Id’ column in the ‘AspNetUserClaims’ table is renamed to 'UserId. The 'Discriminator' column is removed but this is more of an EF change.

NOTE: If you added custom properties on the ApplicationUser class called Email of type string and IsConfirmed of type bool and with the same names, you can delete the property in the ApplicationUser class and the properties will be mapped on to the one in IdentityUser base class. If the properties had different names and types, we would need to add a mapping for them in the DBContext class. Please refer to the following link for more mapping information.

c. Next we need to persist it to the database. Run the command ‘Update-Database -verbose’. The verbose flag lets you view the SQL queries generated. This should pass as expected

If you face issues with migrating to 2.0, please open issues on https://aspnetidentity.codeplex.com.

Leave a Comment
  • Please add 1 and 5 and type the answer here:
  • Post
  • I really can't understand how could you release something that is missing so many basic features!!! Microsoft user management has got worse by any new edition!!! Now you need to release a Version 2 to give us delete user, confirm user and password reset, are you kidding??!!! Did you just lost all your code for Membership and SimpleMembership and had to start from scratch ???!!! Just ridiculous!!!  

  • How does the new relase work with web api?

    In ver 1, you can construct with a factory.

    But the samples for v2 have an extension method to the HttpContext and the Http Context doesn't play nicely with Web Api. What is the way to get the UserManager from an API controller?

  • @mdelgadov : You can include the Microsoft.AspNet.WebApi.Owin package in the project and do a Request.GetOwinContext().GetUserMAnager(). The extension method is defined in the System.Web.Http.Owin.dll

  • The migration fails for me, the migration script generated re-create all talbles instead of alter columns.

    This is probably because I've migrated my database from "Membership" to "Identity".

    Is anyone faced the same issue ?

  • @Nax: Migrating from identity v1 to v2 from a database perspective is related to entity framework. In your case the create tables are being generated instead of the alter tables because looks like your db does not have the '_MigrationsHistory' table which is a snapshot of the identity v1 state. Do let me know if that is the case and I can help you create the '_MigrationsHistory' table

Page 2 of 2 (20 items) 12