【问题标题】:Extending IdentityUser with EF Code-first on VS 2013 SPA AccountController在 VS 2013 SPA AccountController 上使用 EF Code-first 扩展 IdentityUser
【发布时间】:2014-02-11 17:00:27
【问题描述】:

我和一个同学正在使用 Web API 作为我们的后端来开发 Phonegap 应用程序。以前,我们有一个使用 MVC4 模板上的旧成员函数的工作原型。

我们找到了一些关于如何在普通 mvc 控制器上扩展 IdentityUser 的非常好的教程。我们设法让它工作,但转到 VS 2013 SPA 模板附带的 API 帐户控制器,我们没有设法让它工作。在这里简化我们的问题,基本上就是我们想要做的。

假设我们有一个像这样的电影类:

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

我已经像这样创建了 ApplicationUser(取自带有个人帐户的 VS 2013 MVC 模板):

public class ApplicationUser : IdentityUser
{
    public string DisplayName { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<Movie> Films { get; set; }
}

我在 global.asax 的 Application_Start 中添加了这一行:

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

AppDbInit 类:

public class AppDbInit : DropCreateDatabaseAlways<ApplicationDbContext>
{

}

我在 Startup.Auth.cs 中更改了 UserManagerFactory:

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

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

在 AccountController.cs 文件中,我已将所有出现的 IdentityUser 替换为 ApplicationUser。完成后,我开始通过使用 IdentityUser 类的 Microsoft.AspNet.Identity.EntityFramework 中的类文件收到错误,说它无法将 IdentityUser 转换为 ApplicationUser。 这样的实体框架类之一是:

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; }
}

}

我创建了自己的 ApplicationUserLogin 类,我将 User 属性更改为 ApplicationUser 类型之一,但这也不起作用。

这就是我卡住的地方。我通过电子邮件寻求帮助,并被建议编写自己的 UserStore 类来实现 IUserStore 接口。这是扩展 asp.net Identity 创建的 EF 代码优先数据库的唯一方法还是我要关闭?我也许可以只将我的用户帐户保存在一个数据库中,而将其余帐户保存在另一个数据库中,但我更愿意让它发挥作用。

哦,这是我主要使用的文章(就像我说的那样,我设法在标准 mvc 控制器上工作): 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

【问题讨论】:

    标签: c# asp.net entity-framework asp.net-web-api asp.net-identity


    【解决方案1】:

    请参阅 Microsoft 内部团队关于自定义应用程序用户的以下帖子:http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

    【讨论】:

    • 嗯,该教程没有在 accountcontroller 中进行任何更改,您确定这适用于 web api 解决方案吗?
    • 跟 AccountController 没有任何关系。相反,您需要扩展 IdentityUser 模型以包含您想要包含的任何自定义值。
    • 好的,所以你扩展了 IdentityUser 但你让 AccountController 继续只使用 IdentityUser 并且只在你需要扩展属性时才调用 ApplicationUser 类?
    • 您继续在 AccountController 中使用 ApplicationUser。 ASPNet.Identity 将负责填充数据库(并在代码优先的情况下创建它)。
    猜你喜欢
    • 1970-01-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多