Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Identity 2 to 3

Right, so..I think I'm confused!!

I have a few ASP.NET MVC 5 sites running using ASP.NET Identity 2.1 and everything is great. I am creating a new MVC 6 site and I would like the users to use there existing credentials that they use for other systems. I have already tried the below:

  1. Migrating the ASP.NET identity 2 database to 3 (says it cannot as the tables already exist, I thought it would migrate the users in all honesty)
  2. Tried getting MVC 6 to work with ASP.NET Identity 2.1 and failed miserably

I am just wondering what my options are since the documentation is not great on the new version, I get that there are DDL changes in the DB but I was kind of hoping there would be a way for my MVC 5 websites to carry on going as is with the .NET Identity 3 database being backwards compatible with 2.1.

My other option is to upgrade the MVC 5 apps to use Identity 3 which in turn I believe means updating them to MVC 6, which is something I don't really have the resources for, or to have a totally new Identity database (which seems the least fuss option).

Any opinions would be helpful, no doubt I have missed out some detail so happy to fill in the blanks should anyone have any further questions about the setup.

like image 458
MrKobayashi Avatar asked Apr 21 '16 08:04

MrKobayashi


1 Answers

I was doing the same thing, its a good deal of effort - and you will have to run db migrations, but first

The new ASP Identity V3 Tables & Migration

The ASP Identity V3 Tables &  Migration

The new ASP Identity V3 Fields

The fields inside the users table, you can extend this, see below

Configuring the tables In Startup.cs and change services.AddIdentity

   services.AddIdentity<ApplicationUser, IdentityRole<int>>()
    .AddEntityFrameworkStores<ApplicationDbContext, int>()
    .AddDefaultTokenProviders();

In ApplicationContext.cs and change the class to the following signature.

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole<int>, int>

In ApplicationUser.cs and change the class to the following signature.

`public class ApplicationUser : IdentityUser<int> {...}`
  • Given all the changes I found it easier to delete everything in the migrations folder (except the migrations). Go to command prompt on the directory of the project and type/add a migration like so dotnet ef migrations add MyDatabaseHere -o Data\Migrations. (-o Option is to specify target folder and not root folder)
  • for any issues with migration I would simply drop the database and just deploy again.
  • or you can automate with this I have not tried this

I got two migration scripts from EF, I was not sure why... but for more references from github links 1 & 2

like image 159
Transformer Avatar answered Sep 22 '22 18:09

Transformer