Veggerby : IBlog | Low decoupling, highly incoherent

Archive for October 2007

Why has it got to be so hard? Why do you always have to go through a user creation wizard to create an account? Why just not create it as you login like they do on LibraryThing for example?

Well, there is of course always two sides to a coin. The obvious reason is: spam. However all that is needed is an username to "aggregate" your info, then this does not really matter.

So here is a very simple solution to this:

Create a custom MembershipProvider and in the ValidateUser() method first check if the user exists. If not then create the user and then validate him/her. There is of course the requirement that RequiresQuestionAndAnswer and RequiresUniqueEmail must be false.

One way of doing this is to refer a standard membership provider and add some logic to the ValidateUser():

public override bool ValidateUser(string username, string password)
{
    if (GetUser(username, false) == null)
    {
        MembershipCreateStatus status = MembershipCreateStatus.Success;
        MembershipUser user = CreateUser(username, password, null, null, null, true, null, out status);
        if (status != MembershipCreateStatus.Success)
        {
            throw new ProviderException(string.Format("Could not automatically create user: {0}", status));
        }
    }
    return MembershipProvider.ValidateUser(username, password);
}

Download the EasyRegisterMembershipProvider here.

To use this, download, compile and use it in Web.config:

<membership defaultProvider="SignUpProvider">
<providers>
  <clear/>
  <add name="SignUpMembershipProvider" type="Veggerby.Utility.EasySignUpMembershipProvider, Veggerby.Utility" providerName="SqlMembershipProvider"/>
  <add name="SqlMembershipProvider" …/>
</providers>
</membership>

·

Theme Design by devolux.nh2.me