0

I've been able to call Membership.CreateUser which has created the necessary Membership tables within my database, but it hasn't added the newly created user into the Users table and the MembershipCreateStatus = InvalidAnswer - not sure what this means.

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

        }

Configuration:

<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>
litterbugkid
  • 3,534
  • 7
  • 36
  • 54
  • are you seeding users and roles? – Hary Feb 06 '13 at 12:32
  • Just realised the status is InvalidAnswer. I'm guessing that's because I've configured it so that it needs a password question and answer? – litterbugkid Feb 06 '13 at 12:39
  • on creating user successfully it will assign `MembershipCreateStatus = Success` – Hary Feb 06 '13 at 12:40
  • I think I realised that. Do you know how to make it successful? – litterbugkid Feb 06 '13 at 12:40
  • Have you checked after changing this `enablePasswordRetrieval="false"` in `web.config` and also try changing like this ` member. `CreateUser(register.Username, register.Password, "an email address", null, null, true, guid, out status);` – Hary Feb 06 '13 at 12:44
  • Yes my config is above in the answer and it matches what you said. I'll try using null instead of "" – litterbugkid Feb 06 '13 at 12:49
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24029/discussion-between-don-and-neeta) – Hary Feb 06 '13 at 12:50

1 Answers1

0

The answer here are based on our discussion.

I assume that you have this specified this in your web.config as enablePasswordRetrieval="false" from your comment

Then this code will work for you

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
}
Hary
  • 5,690
  • 7
  • 42
  • 79