DiederikEEn
DiederikEEn

Reputation: 743

How to display User with cookie? asp.net

I have the following cookie in my login page:

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

and this on my 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

but I need to have: Response.Cookies("userInfo")("userName") = "s" to be the value of : textboxUser. How can this be done?

I tried : Response.Cookies("userInfo")("userName") = "textboxUser.Text"

But then it just displays that, not the user.

Also, when I fill in : Response.Cookies("userInfo")("userName") = "s" It doesnt display "s" on the default page but : Label

Can someone point me in a good direction?

Upvotes: 0

Views: 667

Answers (1)

Netricity
Netricity

Reputation: 2738

Looks like you're only setting a cookie called userInfo, inside which is an item called userName. You should be checking for the existence of the userInfo cookie then get the items within it, e.g.

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

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

Also, where you're just displaying this cookie's .Value, it will return all keys and their values within the cookie, a bit like a query string.

Upvotes: 1

Related Questions