我有以下代码创建了一个memebrship用户,然后调用其他方法将用户的其他信息插入到我的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
答案 0 :(得分: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
希望它对你有用。