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
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