Just the other day I decided to migrate from the old and reliable Custom Membership Provider to the new Asp.Net Identity 2.0
Mainly for two reasons: I want to integrate social login, and I want to be ready to move to vNext when it will be stable enough to be used in production.
I've started looking at the demo project by Microsoft "WingtipToys", you can download it from here
In order to start with Asp.Net Identity 2, you need to download some packages from NuGet and install them in your new or existing project:
- Microsoft.AspNet.Identity.EntityFramework
- Microsoft.AspNet.Identity.Core
- Microsoft.AspNet.Identity.OWIN
Changing Tables and Fields name
Personally I don't like the naming convention of SQL Tables: AspNetUsers, AspNetRoles,... no way.
So I stucked into the hard process of changing, hard if you don't know how to do.
First of all, to change tables and fields names, you must have created a "Web Project" and not a "Web Site" in Visual Studio.
Why? because you have to install migration for Entity Framework Code First, as the identity SQL tables are created from the code that is in the compiled dll you have previously installed with NuGet.
In order to properly modify the tables and fields name you must add the following code in the onModelCreating of your application DB context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); // This needs to go before the other rules!
modelBuilder.Entity<User>().ToTable("Users").Property(p=>p.Id).HasColumnName("UserId");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims").Property(p=>p.Id).HasColumnName("UserClaimId");
modelBuilder.Entity<IdentityRole>().ToTable("Roles").Property(p=>p.Id).HasColumnName("RoleId");
modelBuilder.Ignore<IdentityUser>();
}
Please note that I have changed the class name ApplicationUser into User, adding some fields to it:
public class User : IdentityUser
{
public string Description { get; set; }
public int OrganizationId { get; set; }
public string FirstName { get; set; }
public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
{
//Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
{
return Task.FromResult(GenerateUserIdentity(manager));
}
}