Request Cookies right after they're being Set

C

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

Joerg Jooss

CDARS said:
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.

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

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 said:
CDARS said:
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.

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

Joerg Jooss

Greg said:
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 ;-)
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

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

Greg Burns

Now don't I look silly. :)

Greg

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


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

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 said:
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 said:
CDARS said:
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.

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

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

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 said:
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 said:
CDARS wrote:
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.

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

Joerg Jooss

CDARS said:
Dear all,

So I understand that when I call


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,
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Cookies Count 2
Can't remove cookies! 6
ASP.Net cookie -> ASP -> ASP.Net 3
create and retrieve cookie? 4
Viewstate issues after move to 2.0 from 1.1 10
Request.Cookies? 3
cookie trouble 3
Problem with cookies 12

Top