473,732 Members | 2,207 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Request Cookies right after they're being Set

Hi all,

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

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

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").val ue 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
8 2340
Hi,
Answer is simple, Request ans Response cookies are not the same...

"CDARS" <cd***@hotmail. com> wrote in message
news:97******** *************** ***@posting.goo gle.com...
Hi all,

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

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

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").val ue 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
CDARS wrote:
Hi all,

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

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

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").val ue 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(objec t sender, System.EventArg s e) {
Response.Cookie s["Test"].Value = DateTime.Now.To ShortDateString ();
Response.Write( Response.Cookie s["Test"].Value);
Response.Write( "<hr />");
Response.Cookie s["Test"].Value =
DateTime.Now.Ad dDays(10).ToSho rtDateString();
Response.Write( Response.Cookie s["Test"].Value);
}
Cheers,

--
Joerg Jooss
jo*********@gmx .net
Nov 18 '05 #3
I get two different dates also in VB.

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

Response.Cookie s("test").Val ue = Now
Response.Write( Request.Cookies ("test").Val ue)

Response.Write( "<hr>")

Response.Cookie s("test").Val ue = Now.AddDays(10)
Response.Write( Request.Cookies ("test").Val ue)

End Sub

Greg

"Joerg Jooss" <jo*********@gm x.net> wrote in message
news:e6******** ******@TK2MSFTN GP12.phx.gbl...
CDARS wrote:
Hi all,

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

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

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").val ue 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(objec t sender, System.EventArg s e) {
Response.Cookie s["Test"].Value = DateTime.Now.To ShortDateString ();
Response.Write( Response.Cookie s["Test"].Value);
Response.Write( "<hr />");
Response.Cookie s["Test"].Value =
DateTime.Now.Ad dDays(10).ToSho rtDateString();
Response.Write( Response.Cookie s["Test"].Value);
}
Cheers,

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

Nov 18 '05 #4
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.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here

Response.Cookie s("test").Val ue = Now
Response.Write( Request.Cookies ("test").Val ue)

Response.Write( "<hr>")

Response.Cookie s("test").Val ue = Now.AddDays(10)
Response.Write( Request.Cookies ("test").Val ue)

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.Cookie s("test").

Cheers,

--
Joerg Jooss
jo*********@gmx .net
Nov 18 '05 #5
Now don't I look silly. :)

Greg

"Joerg Jooss" <jo*********@gm x.net> wrote in message
news:%2******** ********@TK2MSF TNGP09.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.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here

Response.Cookie s("test").Val ue = Now
Response.Write( Request.Cookies ("test").Val ue)

Response.Write( "<hr>")

Response.Cookie s("test").Val ue = Now.AddDays(10)
Response.Write( Request.Cookies ("test").Val ue)

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.Cookie s("test").

Cheers,

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

Nov 18 '05 #6
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@DON T_SPAM_ME_hotma il.com> wrote in message news:<eB******* *******@TK2MSFT NGP11.phx.gbl>. ..
I get two different dates also in VB.

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

Response.Cookie s("test").Val ue = Now
Response.Write( Request.Cookies ("test").Val ue)

Response.Write( "<hr>")

Response.Cookie s("test").Val ue = Now.AddDays(10)
Response.Write( Request.Cookies ("test").Val ue)

End Sub

Greg

"Joerg Jooss" <jo*********@gm x.net> wrote in message
news:e6******** ******@TK2MSFTN GP12.phx.gbl...
CDARS wrote:
Hi all,

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

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

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").val ue 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(objec t sender, System.EventArg s e) {
Response.Cookie s["Test"].Value = DateTime.Now.To ShortDateString ();
Response.Write( Response.Cookie s["Test"].Value);
Response.Write( "<hr />");
Response.Cookie s["Test"].Value =
DateTime.Now.Ad dDays(10).ToSho rtDateString();
Response.Write( Response.Cookie s["Test"].Value);
}
Cheers,

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

Nov 18 '05 #7
Dear all,

So I understand that when I call
response.cookie s("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.c om (CDARS) wrote in message news:<97******* *************** ****@posting.go ogle.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@DON T_SPAM_ME_hotma il.com> wrote in message news:<eB******* *******@TK2MSFT NGP11.phx.gbl>. ..
I get two different dates also in VB.

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

Response.Cookie s("test").Val ue = Now
Response.Write( Request.Cookies ("test").Val ue)

Response.Write( "<hr>")

Response.Cookie s("test").Val ue = Now.AddDays(10)
Response.Write( Request.Cookies ("test").Val ue)

End Sub

Greg

"Joerg Jooss" <jo*********@gm x.net> wrote in message
news:e6******** ******@TK2MSFTN GP12.phx.gbl...
CDARS wrote:
> Hi all,
>
> I have a confusing question on ASP.NET cookies usage:
>
> 1> Response.Cookie s("test").val ue = Now
> 2> Response.Write( Request.Cookies ("test").val ue)
> 3>
> 4> Response.write( "<hr>")
> 5>
> 6> Response.Cookie s("test").val ue = Now.AddDays(10)
> 7> Response.Write( Request.Cookies ("test").val ue)
>
> 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").val ue 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(objec t sender, System.EventArg s e) {
Response.Cookie s["Test"].Value = DateTime.Now.To ShortDateString ();
Response.Write( Response.Cookie s["Test"].Value);
Response.Write( "<hr />");
Response.Cookie s["Test"].Value =
DateTime.Now.Ad dDays(10).ToSho rtDateString();
Response.Write( Response.Cookie s["Test"].Value);
}
Cheers,

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

Nov 18 '05 #8
CDARS wrote:
Dear all,

So I understand that when I call
response.cookie s("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.Coo kies for the first time, it actually copies
all cookies from HttpResponse.Co okies. 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.Cookie s("test").Val ue = Now

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

Response.Write( Request.Cookies ("test").Val ue)

' 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.Cookie s("test").Val ue = Now.AddDays(10)

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

Response.Write( Request.Cookies ("test").Val ue)

' 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.Cookie s 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4599
by: tascien | last post by:
I cannot believe that this code causes an error: Dim MyCookie as string = Request.Cookies("Email").Value Object reference not set to an instance of an object. If the cookie is found, it works, if the cookie is not found, the error above is triggered. and i can't just do this: Dim MyCookie as string = Request.Cookies("Email")
1
11451
by: Alex Nitulescu | last post by:
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
2
2564
by: Shawn Berg | last post by:
Some of my pages in the app I am developing inherit from a BasePage class I have created. I have done plenty of these in the past and they work fine. Now, however, I have an additional requirement. In my BasePage class I need to check the user's browser for a particular cookie, and if certain conditions aren't met, redirect the user to another page. You can see the code for my BasePage class below. The problem lies in the LoggedIn...
4
1682
by: BobRoyAce | last post by:
I have a C# ASP.NET page that I inherited which has two panels on it. It only ever shows one or the other. Initially, it shows panel one. Then, if the user clicks on a certain button, it ultimately shows the other (after a postback, of course). What I need to do it put code into place that will allow code in the Page_Load event, in the case of a PAGE REFRESH (NOTE: Page involved is stored in a FRAME which is part of a FRAMESET), to know...
5
5340
by: Miljana | last post by:
Hi, I have one problem with cookies in ASP.NET application. It seems that I can not retreive cookie from Request.Cookies collection. I put cookie in Response.Cookies collection, and after page post back, when I try to retreive it from Request.Cookies collection, it appears that it does not exists. This problem does not occur on several developing machines we use for developing application, but occurs in another environment (another...
3
2458
by: Arpan | last post by:
An ASPX page has the following code: Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (IsNothing(Request.Cookies("AuthCookie"))) Then Response.Write(Request.Cookies("AuthCookie").Value) Else Response.Write(Cookie 'AuthCookie' Doesn't Exist!) End If End Sub
1
4468
by: Joe | last post by:
In ASP.NET 1.1 I could detected expired form authentication tickets (which closely coincide with my expired session) by checking for the Authentication Cookie when the login screen loads. If the cookie exists, then decrypt the forms auth. ticket and check to see if it is expired. If so display a message to the user letting them know why they are back on the login screen. The code I used was something like this: Dim cookie as HttpCookie...
3
1743
by: user | last post by:
Hi everyone, May I know the standard way of reading in cookies in ASP.net using VB.net? In classic asp, i can do this: If request.cookies("user") = "abc" then blah blah
10
1665
by: _Who | last post by:
Given Request.Cookies and Response.Cookies in asp.net is there any reason to ever use javascript or any other method to use cookies? Thanks
0
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9307
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9235
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6031
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.