How to Authenticate using MVC5RC/RTW with existing database

前端 未结 3 740
误落风尘
误落风尘 2021-01-19 02:36

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 modificat

相关标签:
3条回答
  • 2021-01-19 02:46

    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.

    0 讨论(0)
  • 2021-01-19 02:57

    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?

    0 讨论(0)
  • 2021-01-19 03:08

    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.

    0 讨论(0)
提交回复
热议问题