0

我已经能够调用Membership.CreateUserwhich 在我的数据库中创建了必要的 Membership 表,但是它没有将新创建的用户添加到Users表中并且MembershipCreateStatus = InvalidAnswer- 不确定这意味着什么。

public void Register(Register register)
        {
            var member = new System.Web.Providers.DefaultMembershipProvider();
            object guid = Guid.NewGuid();
            MembershipCreateStatus status;

            member.CreateUser(register.Username, register.Password, "", "", "", true, guid, out status);

        }

配置:

<profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership defaultProvider="DefaultMembershipProvider">
      <providers>
        <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <roleManager defaultProvider="DefaultRoleProvider">
      <providers>
        <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </roleManager>
4

1 回答 1

0

这里的答案是基于我们的讨论。

我假设您已在您的评论中指定了web.configenablePasswordRetrieval="false"一点

然后此代码将为您工作

try
{
    object guid = Guid.NewGuid();
    MembershipCreateStatus status;
    MemberShipUser member = Membership.CreateUser(register.Username, register.Password, "an email address here", null, null, true, guid, out status);
}
catch(MembershipCreateUserException ex)
{
    //handle exceptions
}
于 2013-02-06T13:09:09.473 回答