Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Response.Cookies vs Request.Cookies

Reply
Thread Tools

Response.Cookies vs Request.Cookies

 
 
Alex Nitulescu
Guest
Posts: n/a
 
      02-03-2005
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


 
Reply With Quote
 
 
 
 
Hans Kesting
Guest
Posts: n/a
 
      02-03-2005
Alex Nitulescu wrote:
> 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.

--
Hans Kesting
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments