【问题标题】:Adding more user information to ASP.NET Default Membership Provider向 ASP.NET 默认成员资格提供程序添加更多用户信息
【发布时间】:2013-05-28 20:32:37
【问题描述】:

我的应用程序是一个 MVC4 应用程序,其域模型是在 EF 5 Code First 中创建的。该应用程序需要身份验证/授权,我想使用默认的 ASP.NET Membership Provider。

考虑到这一点,我继续使用 aspnet_reqsql 实用程序添加 ASP.NET 默认成员资格提供程序所需的所有表。

但是,我的应用程序需要存储的用户信息比成员资格提供者默认提供的信息更多。例如:

  • 名字
  • 姓氏
  • 出生日期
  • 地址(分成不同的 列)

这些东西不存在于成员资格提供者表中。所以我继续将所有缺失的列添加到 users 表中,还创建了一个 Addresses 表,并创建了 User 和 Address 之间的关系。

然后我进入我的注册视图模型,添加缺失的数据字段,然后进入 AccountController 并检查注册用户时调用的方法。是这样的:

    //
    // Summary:
    //     Creates a new user profile entry and a new membership account.
    //
    // Parameters:
    //   userName:
    //     The user name.
    //
    //   password:
    //     The password for the user.
    //
    //   propertyValues:
    //     (Optional) A dictionary that contains additional user attributes. The default
    //     is null.
    //
    //   requireConfirmationToken:
    //     (Optional) true to specify that the user account must be confirmed; otherwise,
    //     false. The default is false.
    //
    // Returns:
    //     A token that can be sent to the user to confirm the user account.
    //
    // Exceptions:
    //   System.InvalidOperationException:
    //     The WebMatrix.WebData.SimpleMembershipProvider.Initialize(System.String,System.Collections.Specialized.NameValueCollection)
    //     method was not called.-or-The Overload:WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection
    //     method was not called.-or-The WebMatrix.WebData.SimpleMembershipProvider
    //     membership provider is not registered in the configuration of your site.
    //     For more information, contact your site's system administrator.
    public static string CreateUserAndAccount(string userName, string password, object propertyValues = null, bool requireConfirmationToken = false);

这种方法让我很困惑!我期待看到数据插入数据库的逻辑,以便我可以编辑它并添加使该方法也处理我新添加的字段,但所有这些都丢失了!

我错过了什么?如何实现我想要的注册类型?

【问题讨论】:

    标签: asp.net-mvc-4 ef-code-first asp.net-membership


    【解决方案1】:
    1. 首先,您要使用使用实体框架的新ASP.NET Universal Providers
    2. 如果要添加自定义列,请创建如下所示的新表,并自行根据UserId 检索该自定义数据。

    注意:如果您想使用DefaultMembershipProvider,则不能更改(添加/删除)成员资格提供者创建的任何表的列。

    换句话说,如果您开始添加列,则必须实现CustomMembersipProvider。如果您是 MembershipProvider 的新手,我不建议您这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 2011-03-19
      相关资源
      最近更新 更多