1

我正在Visual Studio 2010用作我的 IDE 并使用我不知道是否可能创建一个简单的网站Visual Basic,但我可以使用会话将刚刚登录到我的 LoginForm 的用户名显示到其他表单吗?

我还不够好,无法理解它,但谁能告诉我,这是在会话中包含值的正确方法吗?,我怎样才能将它显示到另一种形式?

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim connect As String = "Provider=Microsoft.ACE.OleDb.12.0;" & _
        "Data Source=C:\Users\cleanfuel\Documents\Visual Studio 2010\Projects\FinalProject4a2p\FinalProject4a2p\bin\DBFinalProject.accdb"
        Dim query As String
        query = "Select Count(*) From tblAccount Where Username = ? And UserPass = ?"
        Dim result As Integer = 0
        Using conn As New OleDbConnection(connect)
            Using cmd As New OleDbCommand(query, conn)
                cmd.Parameters.AddWithValue("", TxtUser.Text)
                cmd.Parameters.AddWithValue("", txtPass.Text)
                conn.Open()
                result = DirectCast(cmd.ExecuteScalar(), Integer)
            End Using
        End Using

        If result > 0 Then
            Response.Redirect("Menus.aspx")
            Session("User") = TxtUser.Text
            Session("Pass") = txtPass.Text
        Else
            Response.Write("<td>")
            Response.Write("<div align=""center"">")
            Response.Write("<font color='white'>")
            Response.Write("Unable to Login, Invalid Username or Password! </font>")
            Response.Write("</div>")
            Response.Write("</td>")
        End If
    End Sub
4

3 回答 3

3

在您的母版页中设置标签(如果有),将会话中的用户名分配给标签,它将出现在所有页面中。如果您没有母版页,则可以在页面中设置标签(您希望显示用户名),然后将标签Text属性设置为会话中的值。

您在会话中存储值的方式是正确的,Menu.aspx一旦值存储在会话中,您应该重定向到:

If result > 0 Then
            Session("User") = TxtUser.Text
            Session("Pass") = txtPass.Text
            Response.Redirect("Menus.aspx")

....

要访问它们,您可以执行以下操作:

labelUserName.Text = Session("User").ToString()
于 2013-02-18T07:47:19.943 回答
2

使用FormsAuthentication,然后您可以简单地LoginName在表单上放置一个控件,或者从中获取 UserNameHttpContext.Current.User.Identity.Name

于 2013-02-18T08:02:37.717 回答
0

其他用户提供的答案也可以使用,但我找到了这个并成功获得了我想要的结果。

这是我的代码:

Protected Sub ImageButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles ImageButton1.Click
        Dim connect As String = "Provider=Microsoft.ACE.OleDb.12.0;" & _
        "Data Source=C:\Users\cleanfuel\Documents\Visual Studio 2010\Projects\FinalProject4a2p\FinalProject4a2p\bin\DBFinalProject.accdb"
        Dim query As String
        query = "Select Count(*) From tblAccount Where Username = ? And UserPass = ?"
        Dim result As Integer = 0
        Using conn As New OleDbConnection(connect)
            Using cmd As New OleDbCommand(query, conn)
                cmd.Parameters.AddWithValue("", TxtUser.Text)
                cmd.Parameters.AddWithValue("", txtPass.Text)
                conn.Open()
                result = DirectCast(cmd.ExecuteScalar(), Integer)
            End Using
        End Using

        If result > 0 Then
            Dim myCookie As HttpCookie = New HttpCookie("USER")
            myCookie.Value = TxtUser.Text
            Response.Cookies.Add(myCookie)

            Response.Redirect("Menus.aspx")
        Else
            Response.Write("<td>")
            Response.Write("<div align=""center"">")
            Response.Write("<font color='white'>")
            Response.Write("Unable to Login, Invalid Username or Password! </font>")
            Response.Write("</div>")
            Response.Write("</td>")
        End If

    End Sub

我使用 HTTPcookie 而不是 session 因为我不能满足自己,因为它没有显示我想要显示的值,并且它总是一遍又一遍地向我显示相同的错误。

以下是要显示的代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Request.Cookies("USER") Is Nothing Then
            Label7.Text = "No Account Logged In"
        Else
            Dim aCookie As HttpCookie = Request.Cookies("USER")
            Label7.Text = Convert.ToString(Server.HtmlEncode(aCookie.Value))
        End If
    End Sub
于 2013-02-19T06:53:04.317 回答