ASPNET.Identity and MVC5


Question

I am trying to build authentication using the new Identity and MVC5 but I would like to "join" a separate user table to the registration and manage pages. I have read lots on extending the AspNetUser table, but I am desperate to find how to join a separate table. I don't want to just add fields to the AspNetUser table as this project will be a template for other web site applications and each project has different requirements for user tables, therefore I believe it will be more streamline if I can adapt a separate table rather than constantly changing the AspNetUser table.

Any code samples would be greatly appreciated as I learn better by example than explanation!

Alex

Accepted Answer

Since ASP.NET Identity uses EF Code First, you can store your Profile as a separate table by adding a new Code First class and adding it to the DbContext. The following post shows how you can do this http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx


Popular Answer

@Alex this is what have and it does work for me

This creates a custom user class that inherits from IdentityUser just like ApplicationUser, It also defines a virtual class that is the associated profile class that gets linked when the user is created in the database. This can be done here as a custom solution, to make it so that it uses existing tables, you simply replace the objects here with your entity object from the database, and use a connection string that points to the correct database.

  public class CustomUser : IdentityUser
  {
      public virtual CustomUserProfile CustomUserProfile { get; set; }
  }
  public class CustomUserProfile
  {

    [Key]
    public Guid Id
    {
        get { return Guid.NewGuid(); }
        private set { value = Id; }
    }

    public string FirstName { get; set; }
    public string LastName { get; set; }
   //add more items

}
public class ApplicationDbContext : IdentityDbContext<CustomUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }
    public System.Data.Entity.DbSet<CustomUserProfile> CustomUserProfile { get; set; }
}

Now in the controller I have:

 public AccountController()
        : this(new UserManager<CustomUser>(new UserStore<CustomUser>(new ApplicationDbContext())))
    {
    }

    public AccountController(UserManager<CustomUser> userManager)
    {
        UserManager = userManager;
    }

    public UserManager<CustomUser> UserManager { get; private set; }

This is going to create a new UserManager object using your custom user that inherits from IdentityUser just like ApplicationUser Does.

And The method for adding a new user has:

      var user = new CustomUser() { UserName = model.UserName };
                user.CustomUserProfile = new CitadelUserProfile();
                user.CustomUserProfile.FirstName = model.FirstName;


                var result = UserManager.Create(user, model.Password);

If you are still running into problems with the migrations, then you can delete the MDF file and re-run the application and it will recreate the database.





Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why