Re: Request Cookies right after they're being Set

From: Joerg Jooss (joerg.jooss_at_gmx.net)
Date: 09/09/04


Date: Thu, 9 Sep 2004 12:58:29 +0200

CDARS wrote:
> Dear all,
>
> So I understand that when I call
>
>> response.cookies("test") = now
>
> the response object is updated. BUT NOT the request object.
> I really really want to know:
>
> 1) Why it seems to me that in ASP3.0, when I update response object,
> the request object is updated automatically? As I said in the first
> post, a very similar ASP3.0 code will always print two different
> dates.

Don't assume that both frameworks use the same implementation. Granted, the
ASP.NET behaviour is outright bizarre.

> 2) Why for ASP.NET the FIRST post can give two different dates? If
> response and request objects are separated, I would expect two empty
> string from the 1st load...

OK, after digging through the implementation, here's the answer:

When accessing HttpRequest.Cookies for the first time, it actually copies
all cookies from HttpResponse.Cookies. I have no freaking clue what that is
good for (or is it just plain wrong?). It makes no sense to me at all.

Anyway, this it what happens:

Response.Cookies("test").Value = Now

' Creates a new cookie "test" with value DateTime.Now

Response.Write(Request.Cookies("test").Value)

' 1st request: Creates a new cookie collection for HttpRequest, and copies
all response
' cookies to it, prints out DateTime.Now
' 2nd+ request: Gets cookie "test" from request, prints out value stored in
cookie, which
' is DateTime plus 10 days

Response.Write("<hr>")

Response.Cookies("test").Value = Now.AddDays(10)

' Changes value for test cookie, which is contained in both collections

Response.Write(Request.Cookies("test").Value)

' Prints out prints out value stored in cookie "test", which
' is DateTime plus 10 days

> 3) I am actually re-writing a ASP3.0 web app with ASP.NET. The old
> code very often set cookies by response and get the value ALWAYS with
> Request in the same page (or even just in the next line). How should I
> put it in ASP.NET?

Don't create cookies in Response.Cookies before accessing Request.Cookies.
This will prevent this strange copying behaviour.

> 4) Any more funny changes on playing with ASP.NET Cookies?

Hopefully not...

Cheers,

-- 
Joerg Jooss
joerg.jooss@gmx.net 


Relevant Pages

  • Re: HttpWebResponse/HttpWebRequest problem...
    ... you send a request to the server. ... you want to use the cookie to access the server. ... > first response is received, throw an error, so that we can take the cookie ...
    (microsoft.public.dotnet.faqs)
  • Re: Debugging ScreenScrape Code
    ... You are creating cookie container, which creates an empty cookie container. ... You are assigning it to each web request. ... response, you aren't saving the cookies into the cookie container. ... > private string LOGIN_URL; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: What is the difference between Response and Request when I deal with Cookies
    ... if the server is responding to a client... ... but if you want to write or update a cookie then you use Response.Cookies ... > cookies.either Response or Request? ...
    (microsoft.public.cert.exam.mcad)
  • Re: Extracting String Info from WebRequest Cookie
    ... SECRET_PAGE_URL represents a page protected by forms authentication. ... First I request LOGIN_URL first to get the viewstate values ASP.NET ... You can think of this cookie as a ticket. ... >// we don't need the contents of the response, ...
    (microsoft.public.dotnet.languages.csharp)
  • Response/Request/Session Objects
    ... My Session restarts on each postback of my ASP.NET page. ... I set up a test application and have verified that the Request Object has ... This Session ID cookie has a new session ID value ... I verified that when the page exits, the Response object has one cookie with ...
    (microsoft.public.dotnet.framework.aspnet)