Request Cookies right after they're being Set
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.aspnet.

Post a new message to this list...

CDARS
Hi all,

I have a confusing question on ASP.NET cookies usage:

1>    Response.Cookies("test").value = Now
2>    Response.Write(Request.Cookies("test").value)
3>
4>    Response.write("<hr>")
5>
6>    Response.Cookies("test").value = Now.AddDays(10)
7>    Response.Write(Request.Cookies("test").value)

I EXPECT two DIFFERENT dates is printed. The 1st one is today and the
second is 10 days later.

But IN FACT, ASP.NET give me two IDENTICAL dates. From debug mode I
see that AFTER line 6, Request.Cookies("test").value still returns the
old value. Thus line 7 prints the same date as line 2. However when I
look into the cookies file after page load, the cookie is actually
holding the "10-day-later" value.

A very similar ASP 3.0 code gives exactly what I expect (on the same
server, same vitrual root, same IE). I am not even using code-behind
for ASP.NET.

Anyone knows WHY?
and HOW I can fix this?

Many Many Thx!
Reply to this message...
 
    
Joerg Jooss
CDARS wrote:
[Original message clipped]

That's bizarre, the following Page_Load written in C# works fine for me:

private void Page_Load(object sender, System.EventArgs e) {
Response.Cookies["Test"].Value = DateTime.Now.ToShortDateString();
Response.Write(Response.Cookies["Test"].Value);
Response.Write("<hr />");
Response.Cookies["Test"].Value =
DateTime.Now.AddDays(10).ToShortDateString();
Response.Write(Response.Cookies["Test"].Value);
}

Cheers,

--
Joerg Jooss
Click here to reveal e-mail address

Reply to this message...
 
    
Greg Burns
I get two different dates also in VB.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here

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

Response.Write("<hr>")

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

End Sub

Greg

"Joerg Jooss" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Joerg Jooss
Greg Burns wrote:
> I get two different dates also in VB.

OK, so it turns out to be a silly programming error that was not in my C#
version ;-)

[Original message clipped]

Of course this spits out the same two values (after the very first test)...
because it always prints Request.Cookies("test"), not
Response.Cookies("test").

Cheers,

--
Joerg Jooss
Click here to reveal e-mail address

Reply to this message...
 
    
Greg Burns
Now don't I look silly. :)

Greg

"Joerg Jooss" <Click here to reveal e-mail address> wrote in message
news:%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
CDARS
Dear all,

Thanks for all replys.
I should say the first load gives two different dates.
Hit REFRESH at browser and it will always be giving the same date.
Please try that out.

By the way, from your guys experiences, it is ok to request a cookies
in the same page right after it is being set?

"Greg Burns" <Click here to reveal e-mail address> wrote in message news:<Click here to reveal e-mail address>...
[Original message clipped]

Reply to this message...
 
    
CDARS
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.

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

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?

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

Big Thanks, Pros.
Teresa, CDARS Team

Click here to reveal e-mail address (CDARS) wrote in message news:<Click here to reveal e-mail address>...
[Original message clipped]

Reply to this message...
 
    
Joerg Jooss
CDARS wrote:
[Original message clipped]

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

[Original message clipped]

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

[Original message clipped]

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
Click here to reveal e-mail address

Reply to this message...
 
    
puy0
Hi,
Answer is simple, Request ans Response cookies are not the same...

"CDARS" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.DateTime
System.EventArgs
System.Object
System.Web.HttpRequest
System.Web.HttpResponse




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com