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.

  • IsConfirmed... maybe IsEmailVerified (or IsEmailValidated) would be more clear?

  • You might want to update the document to the actual commands:  ‘Enable Migrations’ should be 'enable-migrations' and 'Add Migrations' should be 'add-migrations'

  • @Chris: Thank you. The changes are made

  • Hi there, is a beta or final release already planned for v2?

  • @Maik: You can look at the roadmap for the Identity feature on codeplex for the upcoming releases aspnetidentity.codeplex.com/wikipage

  • Hi guys

    Can't you write one or two articles on how to implement asp.net identity 2.0 on a MVC 5 website that does not have any registration or login facility at all.

    I mean an article that shows how implement this from scratch.

  • @Abdel: 1. Create a new MVC 5 web app. 2. In nuget package manager install these:

    Microsoft.AspNet.Identity.EntityFramework –Version 2.0.0-alpha1 –Pre

    Microsoft.AspNet.Identity.Core -Version 2.0.0-alpha1 –Pre

    Microsoft.AspNet.Identity.OWIN -Version 2.0.0-alpha1 –Pre

    3. Now create/register a user on your /Account/Register page and the database will get created for you with all the new Identity 2.0 pre stuff.

  • I noticed the Add-Migrations Update1 also changed my other properties. I have no idea why?

           public override void Up()

           {

               RenameColumn(table: "dbo.AspNetUserClaims", name: "User_Id", newName: "UserId");

               AddColumn("dbo.AspNetUsers", "IsConfirmed", c => c.Boolean(nullable: false));

               AlterColumn("dbo.AspNetUsers", "UserName", c => c.String(nullable: false));

               AlterColumn("dbo.AspNetUsers", "Email", c => c.String());

               AlterColumn("dbo.AspNetUsers", "DateEdited", c => c.DateTime(nullable: false));

               AlterColumn("dbo.AspNetUsers", "SuccessfulLoginCount", c => c.Int(nullable: false));

               AlterColumn("dbo.AspNetUsers", "DateCreated", c => c.DateTime(nullable: false));

               DropColumn("dbo.AspNetUsers", "Discriminator");

           }

           public override void Down()

           {

               AddColumn("dbo.AspNetUsers", "Discriminator", c => c.String(nullable: false, maxLength: 128));

               AlterColumn("dbo.AspNetUsers", "DateCreated", c => c.DateTime());

               AlterColumn("dbo.AspNetUsers", "SuccessfulLoginCount", c => c.Int());

               AlterColumn("dbo.AspNetUsers", "DateEdited", c => c.DateTime());

               AlterColumn("dbo.AspNetUsers", "Email", c => c.String(maxLength: 254));

               AlterColumn("dbo.AspNetUsers", "UserName", c => c.String());

               DropColumn("dbo.AspNetUsers", "IsConfirmed");

               RenameColumn(table: "dbo.AspNetUserClaims", name: "UserId", newName: "User_Id");

           }

       public class ApplicationUser : IdentityUser

       {

           [MaxLength(64)]

           public string FirstName { get; set; }

           [MaxLength(128)]

           public string LastName { get; set; }

           public DateTime DateEdited { get; set; }

           public int SuccessfulLoginCount { get; set; }        

           public DateTime DateCreated { get; set; }

    }

  • I had this property in my ApplicationUser class - it's now commented out due to the update to ASP.NET Identity 2.0 alpha that I just made:

       public class ApplicationUser : IdentityUser

       {

           // How long can an email address be? A lot of people say 320 characters. A lot of people are wrong. It's 254 characters.

           // http://isemail.info/about

           // And the maximum length of an address is 254 or 256 characters, not 320.

           // stackoverflow.com/.../using-a-regular-expression-to-validate-an-email-address

           // \S+@\S+ stackoverflow.com/.../validate-email-address-in-javascript

           //[MaxLength(254, ErrorMessage = "Max 254 chars.")]

           //public string Email { get; set; }

    ... other properties...

    }

    Can I override Email (from IdentityUser base class) in my ApplicationUser class without breaking anything?

  • @Woot: The EntityFramework package that is installed from the nightly feed might be an old one. Try building the solution from EF codeplex site. Use the assembly in your project and that should resolve unwanted migrations.

  • Yes, definitely... HasConfirmedEmail is a really more Clear... IsConfirmed, for me has a complete different meaning.

  • The Beta is out, to get it Add myget.org/.../aspnetwebstacknightly to the Package Sources.

  • I just updated to 2.0 and upon compiling got an error on doing an update using the following code:

    var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

                               ApplicationUser user = userManager.FindByNameAsync(User.Identity.Name).Result;

                               user.Address = values["address"].ToString();

                               user.City = values["city"].ToString();

                               user.State = values["state"].ToString();

                               user.Country = values["country"].ToString();

                               user.Zip = values["postalCode"].ToString();

                               user.Phone = values["phone"].ToString();

                               userManager.Update<ApplicationUser>(user);

    The error shows at after .Update I need to have <TUser, TKey>.  What should TKey be? Thanks!

  • Just discovered I could change the last line to be:

    userManager.Update(user);

    and it worked.

  • What's going on.  The useful SqlServer Membership was replaced by Simple Membership which in turn, after just about a year, was obsoleted by Asp.Net Identity.  Unfortunately each new model had fewer useful features, 'culminating' with Asp.Net Identity version 1 released in VS2013, without even a delete user feature.  I'm glad you are adding standard features like email confirmation for account registration.  I wish there had been a big red flag in VS2012 and VS2013 warning me about the lack of utility in Simple Membership and Asp.Net Identity.

Page 1 of 2 (20 items) 12