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

asp.net asp.net-identity asp.net-web-api c# entity-framework

Question

My classmate and I are developing a Phonegap app with a Web API as the backend. Prior to this, we had a functional prototype built using an MVC4 template and the previous membership method.

How to extend the IdentityUser on a typical mvc controller is covered in a few really fantastic tutorials that we've found. We were able to make it work, however we were unsuccessful in making the API accountcontroller included with the VS 2013 SPA template work. In essence, what we want to do is simplify this issue.

Suppose our film class looked like this:

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

I built the ApplicationUser in the following manner (using the individual account from the VS 2013 MVC template):

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've added the following line to global.asax's Application Start:

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

The class AppDbInit:

public class AppDbInit : DropCreateDatabaseAlways<ApplicationDbContext>
{

}

In Startup.Auth.cs, I altered the UserManagerFactory as follows:

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());

public static Func<UserManager<ApplicationUser>> UserManagerFactory { get; set; }

I've changed every instance of IdentityUser in the AccountController.cs file to ApplicationUser. After completing that, I began to get issues caused by class files inside of Microsoft.AspNet. Identity.EntityFramework claiming that it cannot convert IdentityUser to ApplicationUser since it uses the IdentityUser class. These entityframework classes include:

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 tried changing the User property to one of type ApplicationUser in my own class ApplicationUserLogin, but it also did not work.

That's where I'm at a loss. When I requested for assistance via email, someone advised that I create my own UserStore class that implements the IUserStore interface. Is it the only method of expanding the EF code-first database created by asp.net Identity, or am I getting close? Even though I could possibly store my user accounts in one database and the rest in another, I would rather make this work.

Oh, and the article I've been using the most is this one: 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 (which I was able to make work on a typical MVC controller).

1
5
1/22/2014 9:24:07 AM

Accepted Answer

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

-1
1/22/2014 12:28:35 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow