0

我有以下代码创建一个会员用户,然后调用其他方法将用户的其他信息插入我的 SQL DB。正在创建用户。信息正在进入我的数据库。一切正常,但由于某种原因,而不是向我的 Literal0.text 发送成功消息,它总是说“已经有一个用户使用此电子邮件地址。”,即使在成功创建用户之后也是如此。我什至添加了对 Session("Created") 的检查以防止代码运行两次,但仍然没有运气。它工作正常,只是在最后给出了错误的信息。

Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If Not Session("Created") = "True" Then
        Literal0.Text = ""
        Dim FName As String = FNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "")
        Dim LName As String = LNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "")
        Username = FName + "." + LName
        Dim SecretQ As String = "Please use the Contact Us page to request a reset password"
        Dim SecretA As String = "ergwergkqejfqeoufwqeofiheowfqpkoadmvnwo"
        Dim psw As Int32 = (TimeOfDay.Hour * TimeOfDay.Minute) * 863490
        Dim firstsix As String = psw.ToString().Substring(0, 6)
        password = FNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "") + firstsix + "!"
        Dim createStatus As MembershipCreateStatus
        Dim newUser As MembershipUser = _
        Membership.CreateUser(Username, password, _
        Email.Text, SecretQ, _
        SecretA, True, _
        createStatus)
        Select Case createStatus
            Case MembershipCreateStatus.Success
                Session("Created") = "True"
                If Not String.IsNullOrEmpty(BlogSettings.Instance.SelfRegistrationInitialRole) Then
                    Dim role As String = Roles.GetAllRoles().FirstOrDefault(Function(r) r.Equals(BlogSettings.Instance.SelfRegistrationInitialRole, StringComparison.OrdinalIgnoreCase))
                    If Not String.IsNullOrEmpty(role) Then
                        Roles.AddUsersToRoles(New String() {Username}, New String() {role})
                    End If
                End If
                Dim pf = AuthorProfile.GetProfile(Username)
                pf = New AuthorProfile(Username)
                pf.DisplayName = Username
                pf.EmailAddress = Email.Text
                pf.FirstName = FNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "")
                pf.LastName = LNameBox.Text.Replace("'", "*1*").Replace("""", "*2*").Replace(" ", "")
                pf.[Private] = True
                pf.Save()
                Literal0.Text = ""
                AddUser()
                Exit Select
            Case MembershipCreateStatus.DuplicateUserName
                Literal0.Text = "There is already a user with this username."
                Exit Select
            Case MembershipCreateStatus.DuplicateEmail
                Literal0.Text = "There is already a user with this email address."
                Exit Select
            Case MembershipCreateStatus.InvalidEmail
                Literal0.Text = "There email address you provided in invalid."
                Exit Select
            Case MembershipCreateStatus.InvalidAnswer
                Literal0.Text = "There security answer was invalid."
                Exit Select
            Case MembershipCreateStatus.InvalidPassword
                Literal0.Text = "The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character."
                Exit Select
            Case Else
                Literal0.Text = "There was an unknown error; the user account was NOT created."
                Exit Select
        End Select
    End If
End Sub
4

1 回答 1

0

我建议从以下部分检查代码:

请给应用程序做一些断点,我们应该检查每次调试点是否会进入以下情况:

Case MembershipCreateStatus.DuplicateEmail
                Literal0.Text = "There is already a user with this email address."
                Exit Select

请确保该电子邮件不存在于 DataTable 中。

有关 MembershipCreateStatus 的更多信息,请参阅以下链接:

http://msdn.microsoft.com/en-us/library/system.web.security.membershipcreatestatus.aspx

希望它对你有用。

于 2013-11-14T10:20:22.243 回答