Response.Cookies vs Request.Cookies

A

Alex Nitulescu

I have the following very simple colde (while learning about cookies and
session state):

Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdAddCookie.Click

Dim strCookieName As String
Dim objRandom As New Random()

strCookieName = "MyCookie" & objRandom.Next(Integer.MaxValue).ToString
Dim objCookie As New HttpCookie(strCookieName)
objCookie.Values("Name") = txtUsername.Text
objCookie.Values("Pass") = txtPassword.Text
objCookie.Expires = #12/25/2005#
Response.Cookies.Add(objCookie)
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

Dim strCookie As String
Dim strKey As String

lblResults.Text = ""
For Each strCookie In Response.Cookies
lblResults.Text &= "Cookie Name = " &
Response.Cookies(strCookie).Name
If Response.Cookies(strCookie).HasKeys Then
For Each strKey In Response.Cookies(strCookie).Values
lblResults.Text &= "<li>" & "Key = " & strKey & "/Value = "
& Response.Cookies(strCookie).Values(strKey)
Next strKey
End If
Next strCookie
End Sub

When I run my project, I see only
Cookie Name = ASP.NET_SessionId

I enter something in my two textboxes (username and password) and I press
AddCookie

Now, after the POST, I see something like:
Cookie Name = MyCookie1328144184
a.. Key = Name/Value = User 1
a.. Key = Pass/Value = NewPass

I expected to create a new cookie every time I run my project (that's why I
append a random number, to change the cookie's name).

This code does NOT work properly. However, if in Page_PreRender I change
Response.Cookies to Request.Cookies everything works fine.
What is the difference between Response and Request in this respect ? They
both implement the cookies collection.

In MSDN I read the definition of a HTTP response: "Gets the intrinsic
response object for the current request." Yeah, that is *very* helpful !
It's like defining "a cat" as being "a form of life born from a cat, not a
dog". I can only assume that I send a request and the server sends a
response.

PS. BTW, where are these cookies stored ? I searched my HD high & low.... no
luck yet. I have all kind of things in "C:\Documents and
Settings\Administrator\Local Settings\Temporary Internet Files\", but no
"MyCookie".

Thanks a lot,
Alex. Nitulescu
 
H

Hans Kesting

Alex said:
I have the following very simple colde (while learning about cookies and
session state):

Private Sub cmdAddCookie_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdAddCookie.Click

Dim strCookieName As String
Dim objRandom As New Random()

strCookieName = "MyCookie" & objRandom.Next(Integer.MaxValue).ToString
Dim objCookie As New HttpCookie(strCookieName)
objCookie.Values("Name") = txtUsername.Text
objCookie.Values("Pass") = txtPassword.Text
objCookie.Expires = #12/25/2005#
Response.Cookies.Add(objCookie)
End Sub

Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender

Dim strCookie As String
Dim strKey As String

lblResults.Text = ""
For Each strCookie In Response.Cookies
lblResults.Text &= "Cookie Name = " &
Response.Cookies(strCookie).Name
If Response.Cookies(strCookie).HasKeys Then
For Each strKey In Response.Cookies(strCookie).Values
lblResults.Text &= "<li>" & "Key = " & strKey & "/Value = "
& Response.Cookies(strCookie).Values(strKey)
Next strKey
End If
Next strCookie
End Sub

When I run my project, I see only
Cookie Name = ASP.NET_SessionId

I enter something in my two textboxes (username and password) and I press
AddCookie

Now, after the POST, I see something like:
Cookie Name = MyCookie1328144184
a.. Key = Name/Value = User 1
a.. Key = Pass/Value = NewPass

I expected to create a new cookie every time I run my project (that's why I
append a random number, to change the cookie's name).

This code does NOT work properly. However, if in Page_PreRender I change
Response.Cookies to Request.Cookies everything works fine.
What is the difference between Response and Request in this respect ? They
both implement the cookies collection.

In MSDN I read the definition of a HTTP response: "Gets the intrinsic
response object for the current request." Yeah, that is *very* helpful !
It's like defining "a cat" as being "a form of life born from a cat, not a
dog". I can only assume that I send a request and the server sends a
response.

PS. BTW, where are these cookies stored ? I searched my HD high & low.... no
luck yet. I have all kind of things in "C:\Documents and
Settings\Administrator\Local Settings\Temporary Internet Files\", but no
"MyCookie".

Thanks a lot,
Alex. Nitulescu

Request.Cookies are the cookies sent along with the "request"
(that is: from browser to server), Response.Cookies are the
cookies get get sent out (from server to browser).

Your cookies (as received by IE, other browsers use different
directories or files) *are* stored in "Temporary Internet Files",
but the filename is usually derived from the servername, *not*
the cookie-name.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top