5

I originally asked this question when Identity was in beta. The classes and interfaces have changed considerably since then and it appears the RTW version has some modifications again over the RC version. In principle I need to achieve the following.

  • authenticate the local login against my usertable tblMembers which contains the userid field and password which are the two items I need to authenticate.
  • have access to my tblMember record/class via the Controller.User property (Prior to MVC5 identity I had achieved this using the membership provider methods.) regardless of if the user logged in via the localuser method or via one of the other OAuth providers (Twitter, Google etc).
  • Ability to display my own custom username despite the login method. Local users login with a userid 1234567 and a password, ideally I would like to display "John Smith (1234567)" regardless of the authentication method (local/Twitter etc)

Initially I'm unsure as to what my memberclass should be inheriting from It appears from the aspIdentitySample project that I should be using IdentityUser?

        public partial class tblMember
        {
            public int id { get; set; }
            public string membership_id { get; set; }
            public string password { get; set; }
            ....other fields
        }

Are there any new or updated examples of integrating your existing database/user tables with the ASP.NET Identity system?

Community
  • 1
  • 1
Tim
  • 7,401
  • 13
  • 61
  • 102
  • 1
    Tim, I'm not sure if my coding practice is getting to old but I still believe one should not be forced to add all these extra tables to an existing database just so we can register/login a user on the system. Why is MS forcing us to use "code first" when we have existing database?? Somebody said to create a referential integrity with the aspnet_users table. Seriously?? I'm starting to feel like a mammoth cause I don't get why MS is pretending MVC is only for brand new applications. – SF Developer Dec 02 '13 at 07:14
  • @eager-to-learn I've had nothing but problems trying to get the new identity system working with an existing DatabaseFirst edmx generated model. It appears that there is very little knowledge on the whole identity system, and there are fundamental changes being done all the time to the architecture. Many of my questions on these subjects remain unresolved. – Tim Dec 02 '13 at 08:30
  • If you look at the proposed answers, it seems one has to patch a system that was not designed for existing databases by forcing the code first to write tables and columns names that we already have on our existing tables. I really don't get why MS seems to totally ignore that there are millions of databases out there that we would love to use with a new MVC application. Not sure why, anyhow good luck in your search. If I find something, I'll poke you and please feel free do the same if you find the way. – SF Developer Dec 03 '13 at 01:58

3 Answers3

4

I am also adding the identity tables to my database. If you create a new web project in visual studio 2013 you will see that now in RTM everything works better than RC plus you will see the following table

  public class ApplicationUser : IdentityUser
  {


   }

So Instead of ApplicationUser you can call your table tblMembers

  public class tblMembers : IdentityUser
  {


   }

your table tblMembers will inherit Id Username Password security stamp and a discriminator column saying this is a tblMemeber

without making custom classes for authentication the easiest thing to do would be just to make the username the combination of your old usernames and userids. Then store the users real name or old username without the user id in a separate column.

have the users register with the built in user login and they can go to manage account and click use another service to log in. This will link the Google account to their regular account, so no matter which one they use it will log them in to the same account. If you have users with connected table information, I suggest you seed your table with all the users with something similar to the register method found in the template.Then just match the new combined username and Id to the old ones and populate data where needed in sql management studio.

Again a lot of issues in RC with extending IdentityUsers have been fixed. Microsoft is already adding more features to the identity user store and this tutorial http://www.windowsazure.com/en-us/develop/net/tutorials/web-site-with-sql-database/ is supposed to be updated soon. I plan on making my own walk through when i'm done changing my database but for now I hope my suggestions even though they are a simpler solution than you might want to implement.

Casey Sebben
  • 677
  • 6
  • 14
1

You can do this easily by modifying the IdentityModel.cs as per the below:

Override OnModelCreating in your DbContext then add the following, this will change AspNetUser table to "Users" you can also change the field names the default Id column will become User_Id.

modelBuilder.Entity<IdentityUser>()
                    .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");

or simply the below if you want to keep all the standard column names:

modelBuilder.Entity<IdentityUser>()
                        .ToTable("Users", "dbo")

Full example below (this should be in your IdentityModel.cs file) i changed my ApplicationUser class to be called User.

public class User : IdentityUser
    {
        public string PasswordOld { get; set; }
        public DateTime DateCreated { get; set; }

        public bool Activated { get; set; }

        public bool UserRole { get; set; }

    }

public class ApplicationDbContext : IdentityDbContext<User>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }

        protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>()
                .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
            modelBuilder.Entity<User>()
                .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
        }
    }

Please note i have not managed to get this working if the current table exists. Also note whatever columns you do not map the default ones will be created.

Hope that helps.

Piotr Stulinski
  • 9,241
  • 8
  • 31
  • 46
  • Perhaps im missing something, but why are there two modelBuilder entity mappings? One to User and IdentityUser? – Tim Oct 28 '13 at 13:17
0

I'm starting to think (partially due to the lack of information in this area), that it may be easier to user the default identity classes, but to provide referential integrity to my own user table from the AspNetUsers table.

If i add a custom linking field into the AspNetUsers table is it possible to access my tables from the Controllers.User property? i.e. Controller.User.tblMember.Orders?

Tim
  • 7,401
  • 13
  • 61
  • 102