By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,617 Members | 1,605 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,617 IT Pros & Developers. It's quick & easy.

Request Cookies right after they're being Set

P: n/a
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!
Nov 18 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi,
Answer is simple, Request ans Response cookies are not the same...

"CDARS" <cd***@hotmail.com> wrote in message
news:97**************************@posting.google.c om...
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!

Nov 18 '05 #2

P: n/a
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,

--
Joerg Jooss
jo*********@gmx.net
Nov 18 '05 #3

P: n/a
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" <jo*********@gmx.net> wrote in message
news:e6**************@TK2MSFTNGP12.phx.gbl...
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,

--
Joerg Jooss
jo*********@gmx.net

Nov 18 '05 #4

P: n/a
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 ;-)

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,

--
Joerg Jooss
jo*********@gmx.net
Nov 18 '05 #5

P: n/a
Now don't I look silly. :)

Greg

"Joerg Jooss" <jo*********@gmx.net> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
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 ;-)

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,

--
Joerg Jooss
jo*********@gmx.net

Nov 18 '05 #6

P: n/a
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" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message news:<eB**************@TK2MSFTNGP11.phx.gbl>...
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" <jo*********@gmx.net> wrote in message
news:e6**************@TK2MSFTNGP12.phx.gbl...
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,

--
Joerg Jooss
jo*********@gmx.net

Nov 18 '05 #7

P: n/a
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

cd***@hotmail.com (CDARS) wrote in message news:<97**************************@posting.google. com>... 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" <greg_burns@DONT_SPAM_ME_hotmail.com> wrote in message news:<eB**************@TK2MSFTNGP11.phx.gbl>...
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" <jo*********@gmx.net> wrote in message
news:e6**************@TK2MSFTNGP12.phx.gbl...
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,

--
Joerg Jooss
jo*********@gmx.net

Nov 18 '05 #8

P: n/a
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
jo*********@gmx.net
Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.