Response.Cookies vs Request.Cookies

Discussion in 'ASP .Net' started by Alex Nitulescu, Feb 3, 2005.

  1. 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
     
    Alex Nitulescu, Feb 3, 2005
    #1
    1. Advertising

  2. Alex Nitulescu

    Hans Kesting Guest

    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
     
    Hans Kesting, Feb 3, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. user
    Replies:
    3
    Views:
    694
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 31, 2007
  2. mhshpk
    Replies:
    0
    Views:
    574
    mhshpk
    Jul 18, 2007
  3. Eric
    Replies:
    1
    Views:
    2,106
    Mark Fitzpatrick
    Dec 28, 2007
  4. _Who
    Replies:
    7
    Views:
    2,768
  5. Cal Who

    Response.cookies vs Request.cookies

    Cal Who, Dec 5, 2009, in forum: ASP .Net
    Replies:
    7
    Views:
    1,553
    Gregory A. Beamer
    Dec 10, 2009
Loading...

Share This Page