0

我有一个使用 WCF 和 Membership.CreateUser 创建新用户的 Silverlight Web 应用程序。

如果我成功创建一个用户,然后再创建另一个用户,我会收到两次“用户成功创建”状态消息。或者,如果我第一次尝试创建新用户失败,比如因为用户名已经存在,那么任何进一步的尝试都会多次返回状态消息。

我的 WCF 服务代码

[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;
}

我对方法的调用

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

我的活动完成

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

最后是 GetErrorMessage 方法

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.";
    }
}

关于为什么会发生这种情况的任何想法?发送

尼尔

4

1 回答 1

1

我猜你CreateNewUserCompleted在调用方法之前添加了处理程序CreateNewUserAsync,然后永远不会删除它。你的tmsClient_CreateNewUserCompleted方法应该看起来像这样才能让它正常工作:

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);
}
于 2012-04-10T19:47:43.930 回答