如何用cookie显示用户? asp.net

时间:2013-05-07 14:39:43

标签: asp.net cookies

我的登录页面中有以下cookie:

       Response.Cookies("userInfo")("userName") = "s"
       Response.Cookies("userInfo")("lastVisit") = DateTime.Now.ToString()
       Response.Cookies("userInfo").Expires = DateTime.Now.AddDays(1)
       Response.Redirect("default.aspx")

这是我的default.aspx:

       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

但我需要:Response.Cookies("userInfo")("userName") = "s"为:textboxUser的值。怎么办呢?

我试过了:Response.Cookies("userInfo")("userName") = "textboxUser.Text"

然后它只是显示,而不是用户。

另外,当我填写时:Response.Cookies("userInfo")("userName") = "s"它不会在默认页面上显示“s”但是:Label

有人能指出我的方向吗?

1 个答案:

答案 0 :(得分:1)

您似乎只设置名为userInfo的Cookie,其中包含名为userName的项目。您应检查是否存在userInfo Cookie然后获取其中的项目,例如

Dim aCookie As HttpCookie = Request.Cookies("userInfo")

If aCookie IsNot Nothing Then
    Label1.Text = Server.HtmlEncode(aCookie("userName"))
End If

此外,如果您只是显示此Cookie的.Value,它将返回Cookie中的所有键及其值,有点像查询字符串。