After creating a user or if creation fails, any more attempts at saving returns MembershipCreteStatus more than once

StackOverflow https://stackoverflow.com/questions/10089634

Question

I have a Silverlight web app using WCF and Membership.CreateUser to create new users.

If I successfully create a user, then create another user, I get the "User sucessfully created" status message twice. Or if my first attempt at creating a new user fails, say because the username already exists, any further attempts brings back the status messages more than once.

My WCF service code

[OperationContract]
public MembershipCreateStatus CreateNewUser(string userName, string password, string email, string securityQuestion, string securityAnswer)
{
   MembershipCreateStatus status = new MembershipCreateStatus();
   MembershipUser newUser = Membership.CreateUser(userName, password, email, securityQuestion, securityAnswer, true, out status);

   return status;
}

My call to the method

tmsClient.CreateNewUserCompleted += new EventHandler<CreateNewUserCompletedEventArgs>(tmsClient_CreateNewUserCompleted);
tmsClient.CreateNewUserAsync(nu.UserName, nu.Password, nu.Email, nu.SecurityQuestion, nu.SecurityAnswer);

My event completed

void tmsClient_CreateNewUserCompleted(object sender, CreateNewUserCompletedEventArgs e)
{
    MembershipCreateStatus status = e.Result;
    string result = GetErrorMessage(status);
    MessageBox.Show(result);
}

And lastly the GetErrorMessage method

public string GetErrorMessage(MembershipCreateStatus status)
{
    switch (status)
    {
        case MembershipCreateStatus.Success:
            return "The user was successfully created.";

        case MembershipCreateStatus.DuplicateUserName:
            return "Username already exists. Please enter a different user name.";

        case MembershipCreateStatus.DuplicateEmail:
            return "A username for that e-mail address already exists. Please enter a different e-mail address.";

        case MembershipCreateStatus.InvalidPassword:
            return "The password provided is invalid. Please enter a valid password value.";

        case MembershipCreateStatus.InvalidEmail:
            return "The e-mail address provided is invalid. Please check the value and try again.";

        case MembershipCreateStatus.InvalidAnswer:
                return "The password retrieval answer provided is invalid. Please check the value and try again.";

        case MembershipCreateStatus.InvalidQuestion:
                return "The password retrieval question provided is invalid. Please check the value and try again.";

        case MembershipCreateStatus.InvalidUserName:
                return "The user name provided is invalid. Please check the value and try again.";

        case MembershipCreateStatus.ProviderError:
            return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

        case MembershipCreateStatus.UserRejected:
            return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";

        default:
            return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
    }
}

Any ideas as to why this is happening? Tx

Neill

Was it helpful?

Solution

I guess you add the handler for CreateNewUserCompleted just before calling the CreateNewUserAsync method and then never remove it. Your tmsClient_CreateNewUserCompleted method should look sth like this to get this working properly:

void tmsClient_CreateNewUserCompleted(object sender, CreateNewUserCompletedEventArgs e)
{
    tmsClient.CreateNewUserCompleted -= new EventHandler<CreateNewUserCompletedEventArgs>(tmsClient_CreateNewUserCompleted);
    MembershipCreateStatus status = e.Result;
    string result = GetErrorMessage(status);
    MessageBox.Show(result);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top