none
有关asp.net中的membership类 RRS feed

  • 问题

  • 请教高手,帮忙解决一下,小妹初次做程序,拜托各们帮帮忙
    为什么在进入登陆页面的时候如果不重新注册新的用户就会出现错误,而当你注册一个新的用户之后再次登陆就不会有错误发生呢
    错误原因如下:
          用户代码未处理ArgumentException
          提供程序的用户密钥无效。该密钥必须是 System.Guid 类型。 参数名: providerUserKey

    注册用户界面中的注册按钮代码如下:
    Partial Class CreatUser
        Inherits System.Web.UI.Page 
        Protected Sub submit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles submit.Click
            Dim userName1 As String = userName.Text
            Dim password1 As String = password.Text
            Dim email1 As String = Email.Text

            Dim passwordQuestion As String = question.SelectedValue
            Dim passwordAnswer As String = Answer.Text
            Dim result As MembershipCreateStatus
            Membership.CreateUser(userName1, password1, email1, passwordQuestion, passwordAnswer, True, result)
            lblResults.Visible = True

            Select Case result
                Case MembershipCreateStatus.Success
                    userName.Text = Nothing
                    password.Text = Nothing
                    Email.Text = Nothing

                    question.SelectedIndex = -1
                    Answer.Text = Nothing
                    lblresults.Text = "恭喜您注册成功!!"
                Case MembershipCreateStatus.InvalidUserName
                    lblresults.Text = "用户名格式错误,请您重新输入一个新的用户名!"
                Case MembershipCreateStatus.InvalidPassword
                    lblresults.Text = "密码要求长于7位,并且需要至少包括一个特殊字符."
                Case MembershipCreateStatus.InvalidEmail
                    lblresults.Text = "邮件地址格式错误,请您重新输入一个新的邮件地址!"
                Case MembershipCreateStatus.InvalidQuestion
                    lblresults.Text = "The Quesstion format was invalid  Please enter a different Question."
                Case MembershipCreateStatus.InvalidAnswer
                    lblResults.Text = "the password answer format was invalid  Please enter a different Answer."
                Case MembershipCreateStatus.DuplicateUserName
                    lblresults.Text = "用户名已经被注册过,请输入一个新的用户名."
                Case MembershipCreateStatus.DuplicateEmail
                    lblresults.Text = "邮件地址已存在,请您输入新的邮件地址!"
                Case Else
                    lblresults.Text = "您创建的用户发生错误!"
            End Select
        End Sub
    End Class
    登陆界面中登陆按钮代码如下:
    Partial Class _Default
        Inherits System.Web.UI.Page

        Protected Sub loginIn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles loginIn.Click
            Dim userid As String = userName.Text
            Dim txtpassword As String = PassWord.Text

            If (Membership.ValidateUser(userid, txtpassword)) Then
                If Not (Request.QueryString("returnUrl") Is Nothing) Then
                    FormsAuthentication.RedirectFromLoginPage(userid, False)
                Else
                    FormsAuthentication.SetAuthCookie(userid, False)
                    Response.Redirect("userinformation.aspx")
                End If
            Else
                lblResults.Visible = True
                lblresults.Text = "登陆失败,请您重新登陆!."

                If (Not Membership.GetUser(userName) Is Nothing) Then
                    If (Membership.GetUser(userName).IsLockedOut = True) Then
                        lblResults.Text = lblResults.Text & "<b>Your account has been locked out.</b>"
                    End If
                End If
            End If
        End Sub

    • 已移动 Raymond TangModerator 2009年11月2日 6:53 ASP.NET related (发件人:.NET Framework 一般性问题讨论区)
    2009年11月2日 3:25

答案

  • 如果你是用的自带的SqlMembershipProvider
    GetUser传入的用户标识是SqlDbType.UniqueIdentifier 也就是GUID
    command.Parameters.Add(this.CreateInputParam("@UserId", SqlDbType.UniqueIdentifier, providerUserKey));
    但是你这里GetUser传入的却是用户名 而不是GUID因而会出错

    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~
    2009年11月2日 6:52
    版主

全部回复

  • 如果你是用的自带的SqlMembershipProvider
    GetUser传入的用户标识是SqlDbType.UniqueIdentifier 也就是GUID
    command.Parameters.Add(this.CreateInputParam("@UserId", SqlDbType.UniqueIdentifier, providerUserKey));
    但是你这里GetUser传入的却是用户名 而不是GUID因而会出错

    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!My blog~~~
    2009年11月2日 6:52
    版主
  • 多谢,可不可以告诉我如何改呢?本人比较愚顿!
    2009年11月6日 3:29