Extending IdentityUser with EF Code-first on VS 2013 SPA AccountController

13,393

See the following post from the internal team at Microsoft on customising the Application User: http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

Share:
13,393
Ashkan Hovold
Author by

Ashkan Hovold

Updated on June 17, 2022

Comments

  • Ashkan Hovold
    Ashkan Hovold almost 2 years

    Me and a classmate are working on a Phonegap application using a Web API as our backend. Previously we had a working prototype using the old membership function on a MVC4 template.

    We've found some pretty good tutorials on how to extend the IdentityUser on a normal mvc controller. We managed to get that to work but going over to the API accountcontroller that comes with the VS 2013 SPA template, we've not managed to get it to work. To simplify our problem here is basicly what we want to do.

    Lets say we have a Movie class like this:

    public class Movie
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public virtual ApplicationUser User { get; set; }
    }
    

    I've created the ApplicationUser like this(taken from the VS 2013 MVC template with individual account):

    public class ApplicationUser : IdentityUser
    {
        public string DisplayName { get; set; }
    }
    
    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    
        public DbSet<Movie> Films { get; set; }
    }
    

    I have added this line in Application_Start on global.asax:

    Database.SetInitializer<ApplicationDbContext>(new AppDbInit());
    

    The AppDbInit class:

    public class AppDbInit : DropCreateDatabaseAlways<ApplicationDbContext>
    {
    
    }
    

    I changed the UserManagerFactory in Startup.Auth.cs:

    UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
    
    public static Func<UserManager<ApplicationUser>> UserManagerFactory { get; set; }
    

    In the AccountController.cs file I've replaced all occurrences of IdentityUser with ApplicationUser instead. Once that was done, I started to get errors by class files inside Microsoft.AspNet.Identity.EntityFramework that are using the IdentityUser class saying it cannot convert IdentityUser to ApplicationUser. One of such entityframework classes is:

    namespace Microsoft.AspNet.Identity.EntityFramework
    {
    public class IdentityUserLogin
    {
        public IdentityUserLogin();
    
        public virtual string LoginProvider { get; set; }
        public virtual string ProviderKey { get; set; }
        public virtual IdentityUser User { get; set; }
        public virtual string UserId { get; set; }
    }
    

    }

    I created my own class ApplicationUserLogin where I changed the User property to one of type ApplicationUser, that did not work either.

    This is where I'm stuck. I e-mailed and asked for help and was suggested to write my own UserStore class implementing the IUserStore interface. Is that the only way to extend the EF code-first database that asp.net Identity creates or am I close? I could perhaps just keep my user accounts in one database and the rest in another but I would prefer to get this to work.

    Oh and this is the article I've been using mainly (which like I said I managed to get workin on a standard mvc controller): http://blogs.msdn.com/b/webdev/archive/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx