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

Ashkan Hovold

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

Brendan Green

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在VS 2013 SPA AccountController上以EF Code-first扩展IdentityUser

来自分类Dev

使用 EF Core Code First 和 IdentityUser 的 .Net Core API

来自分类Dev

在VS2013项目中使用EF 6.0.1

来自分类Dev

如何优化我的EF Code First查询?

来自分类Dev

EF Code First Migration无法编译

来自分类Dev

子类中的 EF Code First 实体列

来自分类Dev

EF Code First 迁移 - 属性迁移

来自分类Dev

EF Code First 外键关系

来自分类Dev

VS2013(RTW):SPA模板与MVC5模板中的身份验证差异?

来自分类Dev

EF Code First 6.1将ID从Int更改为Long

来自分类Dev

EF Code First Fluent API-级联删除

来自分类Dev

通过EF 6 / Code First键查找本地缓存的实体

来自分类Dev

EF Code First多个父母的单个外键

来自分类Dev

EF Code First迁移创建额外的外键

来自分类Dev

EF Code First Fluent API,指定外键属性

来自分类Dev

从EF 6.0 Code First调用存储过程不会插入记录

来自分类Dev

无法将值NULL插入EF Code First列

来自分类Dev

EF Code First回滚数据库表设计

来自分类Dev

通过EF Core + Code First +迁移创建数据库

来自分类Dev

EF Code First 1:0..1关系共享主键

来自分类Dev

WebApi安全-使用Code First和EF定制用户模型

来自分类Dev

从EF 6.0 Code First调用存储过程不会插入记录

来自分类Dev

EF6 Code First预生成的C#视图

来自分类Dev

在EF Code First迁移中使用自定义逻辑

来自分类Dev

EF6 Code First多对多,无集合

来自分类Dev

Code First EF6是否无法检索内部对象?

来自分类Dev

在集合中获取类实例-EF Code First Fluent API

来自分类Dev

使用EF Code First,如何包括嵌套的嵌套数据

来自分类Dev

EF Code First - 具有子实体的序列号

Related 相关文章

热门标签

归档