Attempting to change table names in ASP.NET Identity 2.0

I want to change the table names used by ASP.NET Identity 2.0. I have seen various similar questions, but nothing that solves my problem. For example, this type of solution: http://www.goatly.net/customizing-table-names-for-identitydbcontextwithcustomuser-data-contexts seems to depend on the First (?) Code. Anyway, implementing OnModelCreating does nothing for me. I basically renamed AspNetUsers and similar tables to my own names and want to be able to use them. I have an existing EF context (MyContext) that I want to use, so I changed it to get it from IdentityDbContext (editing t4 template) and then in Startup.Auth:

UserManagerFactory = () => { var context = new MyContext(); Database.SetInitializer<MyContext>(new IdentityDbInitializer()); var userStore = new UserStore<IdentityUser>(context){ DisposeContext = true }; return new UserManager<IdentityUser>(userStore); }; 

But the problem occurs when I try to log in to get "I can not contact the server." I assume this is because my UserStore has no way of knowing which user tables are in my context. I was hoping that the OnModelCreating code would do this, but I fog on this. I believe that UserStore is still looking for the table "AspNetUsers", although I do not know where these names come from. I am also worried about changing the t4 template for my context - is this what I should extract from IdentityDbContext?

Any help is greatly appreciated.

+8
c # entity-framework asp.net-identity-2
source share
2 answers

A few steps:

  • Install NuGet Package: Microsoft.AspNet.Identity.EntityFramework
  • Add connection string to your web.config / app.config file

Now you should define your custom Database Context :

 public class MyContext : IdentityDbContext { public MyContext() : base(<connection string name>) { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<IdentityUser>() .ToTable("Users"); modelBuilder.Entity<IdentityRole>() .ToTable("Roles"); modelBuilder.Entity<IdentityUserRole>() .ToTable("UserRoles"); modelBuilder.Entity<IdentityUserClaim>() .ToTable("UserClaims"); modelBuilder.Entity<IdentityUserLogin>() .ToTable("UserLogins"); } } 

As you can see, I used DbModelBuilder to map all objects to new tables.

  • Open NuGet Package Manager Console
  • Run the Enable-Migrations command

It will create a Migrations folder with the Configuration.cs configuration file.

It should look something like this:

 internal sealed class Configuration : DbMigrationsConfiguration<ConsoleApplication1.Models.MyContext> { public Configuration() { AutomaticMigrationsEnabled = false; } protected override void Seed(ConsoleApplication1.Models.MyContext context) { } } 

In the constructor, change the AutomaticMigrationsEnabled property to true .

  • Open NuGet Package Manager Console
  • Run the Update-Database Command

It should run a script to create new tables.

You can customize your entities (and identifiers) by creating your own class for each specific interface.

 public class MyUser : IdentityUser<string, MyUserLogin, MyUserRole, MyUserClaim> { } 

Since you are using Owin, you can define your UserStore:

 public class MyUserStore: UserStore<MyUser, MyRole, string, MyUserLogin, MyUserRole, MyUserClaim> { public MyUserStore(MyContext context) : base(context) { } } 

and your UserManager implementation:

 public class ApplicationUserManager : UserManager<ASPNETIdentity2.Models.MyUser, string> { public ApplicationUserManager(IUserStore<ASPNETIdentity2.Models.MyUser, string> store) : base(store) { } public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context) { var manager = new ApplicationUserManager(new MyUserStore(context.Get<MyContext>())); manager.UserValidator = new UserValidator<MyUser, string>(manager) { AllowOnlyAlphanumericUserNames = false, RequireUniqueEmail = true }; manager.PasswordValidator = new PasswordValidator() { RequiredLength = 5, RequireNonLetterOrDigit = false, // true // RequireDigit = true, RequireLowercase = false, RequireUppercase = false, }; return (manager); } } 

And your Owin.Startup should look something like this:

 public class Startup { public void Configuration(IAppBuilder app) { app.CreatePerOwinContext(MyContext.Create); app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); } } 

If you want to take a look at a custom implementation, you can check out my GitHub repository with a simple working solution on ASP.NET MVC.

UPDATE:

In this solution, there is another project with minimal setup; basically you need to define your context ( IdentityDbContext ) if you only want to rename your tables.

The project can be found here .

+16
source share

Identification 2 with Entity Framework assumes Code First. This project seems to do what I need: https://github.com/cbfrank/AspNet.Identity.EntityFramework

0
source share

All Articles