Asynchronous
Asynchronous

Reputation: 3977

Using additional validation with ASP.NET Membership Create Status

I personally find vague error messages a pain. In my new practice project I am trying to add additional validation to the user input. However, ASP.NET MembershipCreateStatus has Members; e.g. InvalidEmail that takes care of some of these areas when the CreateUser Method is used.

I can do an if statement or Regex on the submitted email before the input reaches the Membership.CreateUser Method. However, I am not sure where to start with this:

I know this question is a little subjective but I hate to find out later that there is a better way, and if you are just starting up, it can be a lifesaver.

Should I create an additional validation Method to check for very specific formatting errors in the email, in addition to the system checking. As you can see the error message does not say why it is invalid.

Also is it possible to create my own enum for example, email contains underscore, which I can attached to the MembershipCreateStatus? If so how?

Here is my sample code, abbreviated:

public string GetErrorMessage(MembershipCreateStatus status)
{

switch (status)
{

case MembershipCreateStatus.InvalidEmail:
return "The e-mail address provided is invalid.";

default:
return "Go to jail!!";
}
}

protected void btnSubmit_Click(object sender, EventArgs e)
{
try
{
Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtEmail.Text);
}
catch (MembershipCreateUserException ex)
{
ltrStatusMsg.Text = GetErrorMessage(ex.StatusCode);
}
catch (HttpException ex)
{
ltrStatusMsg.Text = ex.Message;
}
}

Thanks my friends.

Upvotes: 0

Views: 633

Answers (2)

to StackOverflow
to StackOverflow

Reputation: 124696

There are lots of ways to skin a cat. Certainly you can do your own validation before calling Membership.CreateUser. Your validation method could throw a MembershipCreateUserException and reuse one of the existing status codes, or throw a different exception. The exception you throw could be a custom exception, or you could reuse an existing exception type such as ValidationException.

try
{
    ValidateUser(txtUserName.Text, txtPassword.Text, txtEmail.Text);
    Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtEmail.Text);
}
catch (MembershipCreateUserException ex)
{
    ltrStatusMsg.Text = GetErrorMessage(ex.StatusCode);
}
catch (ValidationException ex)
{
    ltrStatusMsg.Text = ex.Message;
}
catch (Exception ex)
{
    // log exception ...

    ltrStatusMsg.Text = "An unexpected error occurred while creating the user";
}

...
private void ValidateUser(...)
{
    if (... not valid ...) throw new ValidationException("some suitable message");
}

Upvotes: 1

Mark Redman
Mark Redman

Reputation: 24515

In my CustomMembershipProvider I don't use the CreateUser method, I have a CreateUser method as part of my User business object code with an output parameter (Custom Enum) with the various invalid (or valid) result.

Upvotes: 0

Related Questions