显示登录用户asp.net

时间:2013-04-25 08:37:44

标签: asp.net login

我在asp.net上做了一个小的自定义登录页面,参见代码:

        Dim strCon As String = ConfigurationManager.ConnectionStrings("Goed").ConnectionString


        'Create Connection String And SQL Statement
        Dim strSelect As String = "SELECT COUNT(*) FROM tbl_LogIn WHERE Gebruiker = @Gebruiker AND Wachtwoord = @Wachtwoord"

        Dim con As New SqlConnection(strCon)
        Dim cmd As New SqlCommand()
        cmd.Connection = con
        cmd.CommandType = CommandType.Text
        cmd.CommandText = strSelect

        Dim Gebruiker As New SqlParameter("@Gebruiker", _
                                          SqlDbType.VarChar)
        Gebruiker.Value = TxtUs.Text.Trim().ToString()
        cmd.Parameters.Add(Gebruiker)

        Dim Wachtwoord As New SqlParameter("@Wachtwoord", _
                                           SqlDbType.VarChar)
        Wachtwoord.Value = TxtPw.Text.Trim().ToString()
        cmd.Parameters.Add(Wachtwoord)


        con.Open()
        Dim result As Integer = DirectCast(cmd.ExecuteScalar(), Int32)
        con.Close()

        If result >= 1 Then
            Response.Redirect("default.aspx")
        Else
            lblMsg.Text = "Gebruikers naam en of wachtwoord kloppen niet"
        End If

    End Sub

如您所见,它指向Default.aspx。

在我的defaults.aspx页面上,我有一个标题。在这个标题中我想要一个小标签来记录登录用户:Hello [User]如何做到这一点?

2 个答案:

答案 0 :(得分:2)

使用会话

导演到新页面时(在Login.aspx-in按钮的onClick事件中)

Session["valueName"]=value;

在新页面(在您的情况下为default.aspx)中使用:

Label1.Text=Session["valueName"].ToString();

或者您也可以使用Cookie:

创建

Response.Cookies("userInfo")("userName") = "DiederikEEn"
Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)

<强> READING:

If Not Request.Cookies("userName") Is Nothing Then
    Label1.Text = Server.HtmlEncode(Request.Cookies("userName").Value)
End If

If Not Request.Cookies("userName") Is Nothing Then
    Dim aCookie As HttpCookie = Request.Cookies("userName")
    Label1.Text = Server.HtmlEncode(aCookie.Value)
End If

更多信息:

  1. Cookies
  2. Sessions

答案 1 :(得分:0)

如果您可以在母版页中创建标题,则可以在其中添加Hello [User]并调用会话。