Quantcast
Channel: Category Name
Viewing all articles
Browse latest Browse all 10804

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

$
0
0

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 Migrations 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.


Viewing all articles
Browse latest Browse all 10804

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>