[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Cookie expiration fails

Posted on 2011-05-02
13
Medium Priority
?
724 Views
Last Modified: 2012-05-11
I have this line of code:
Request.Cookies["CurrentUser"].Expires = DateTime.Now.AddMinutes(20);

Open in new window



I suppose it has to add 20 minutes to the cookie which resides on the client.

Now, if I do something like this:

RCookie = Request.Cookies["CurrentUser"];

Open in new window


what should be  Rcookie's expiration? Ithought it would be something 20 minutes after from when I saved "Currentuser" cookie.

BUT
it shows me {1/1/0001 12:00:00 AM} what does that even mean?
0
Comment
Question by:TheCommunicator
13 Comments
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 1776 total points
ID: 35509624
Hi buddy, that is fine, the browser dont send back to the server the cookie expiration date, you see 1/1/0001 12:00:00 AM on the Expires property because it is the default value of DateTime struct.
0
 
LVL 21

Expert Comment

by:Alfred A.
ID: 35509625
If you change the value of a cookie, the expiration date is set back to the 01-Jan-0001 12:00:00 AM.  Check the link below.  Check the Cookie Expiration section.

http://www.codeproject.com/KB/aspnet/aspnetcookies.aspx
 
I hope this helps.
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 1776 total points
ID: 35509629
If you want to make sure of this, install firebug on FireFox, then in the Cookies panel look for your cookie, there will be all the cookie info that you set.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 1776 total points
ID: 35509637
Also you can use Fiddler to see the traffic between your web browser and server:
http://www.fiddler2.com
Must have tool!
0
 
LVL 11

Assisted Solution

by:SAMIR BHOGAYTA
SAMIR BHOGAYTA earned 224 total points
ID: 35509900
Hi, try this example

HttpCookie myCookie = new HttpCookie("MyTestCookie");
DateTime now = DateTime.Now;

// Set the cookie value.
myCookie.Value = now.ToString();
// Set the cookie expiration date.
myCookie.Expires = now.AddMinutes(1);

// Add the cookie.
Response.Cookies.Add(myCookie);

Response.Write("<p> The cookie has been written.");
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 1776 total points
ID: 35512356
Also, Im not sure if your are creating your cookie in the correct way, in your case I will do that in this way:
HttpCookie cookieCurrentUser = Request.Cookies["CurrentUser"];
if (cookieCurrentUser == null)
    cookieCurrentUser = new HttpCookie("CurrentUser");

cookieCurrentUser.Expires = DateTime.Now.AddMinutes(20);
Response.Cookies.Add(cookieCurrentUser);

Open in new window

0
 

Author Comment

by:TheCommunicator
ID: 35515264
Ok Guys, may be my little lack of Web architecture also playing a major role here but just for the sake of the question when I do something like this:

HttpCookie cookieCurrentUser = Request.Cookies["CurrentUser"]

Open in new window


Server is requesting a Cookie with the name "CurrentUser" and using it to instantiate a local instance called cookieCurrentUser. Right?

So, would that be a correct statement that cookieCurrentUser gets everything from the client except expiration date?
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 1776 total points
ID: 35515439
Hi, when you web browser make a request, it always send all the valid cookies on the requested path, so  when you say "Server is requesting a Cookie with the name "CurrentUser"" it is already on the server, also, the unique data sent by the web browser to the server is the cookie name and it value, just that.
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 1776 total points
ID: 35515728
Here a real example:

1. When you navigate to an url

A. The web browser request:
GET http://www.mywebsite.com/Default.aspx HTTP/1.1
Host: localhost:8947
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive

Open in new window

Notice there is not cookies!



B. The web server response:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 03 May 2011 18:02:55 GMT
X-AspNet-Version: 4.0.30319

Set-Cookie: CurrentUser=123; expires=Tue, 03-May-2011 18:22:54 GMT; path=/

Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 2853
Connection: Close

Open in new window

Here your cookie! (Set-Cookie: CurrentUser=123; expires=Tue, 03-May-2011 18:22:54 GMT; path=/)


2. Then, you press a submit button on that page:

A. Your web browser post data on that path:
POST http://www.mywebsite.com/Default.aspx HTTP/1.1
Host: localhost:8947
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:2.0.1) Gecko/20100101 Firefox/4.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.mywebsite.com/Default.aspx

Cookie: CurrentUser=123

Content-Type: application/x-www-form-urlencoded
Content-Length: 638

Open in new window

The web browser send back the cookie (just name and value) to the server (Cookie: CurrentUser=123)


B. The web server response:
HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 03 May 2011 18:03:30 GMT
X-AspNet-Version: 4.0.30319

Set-Cookie: CurrentUser=123; expires=Tue, 03-May-2011 18:23:29 GMT; path=/

Cache-Control: private
Content-Type: text/html; charset=utf-8
Content-Length: 2901
Connection: Close

Open in new window

Notice the updated expiration date of the cookie (Set-Cookie: CurrentUser=123; expires=Tue, 03-May-2011 18:23:29 GMT; path=/)


I hope this give you a better idea of how this work.
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 1776 total points
ID: 35515760
In my last example the cookie value is "123", just for demo purposes.
0
 

Author Comment

by:TheCommunicator
ID: 35516387
To conclude: When server asks for Cookie, Web Browser is not capable of sending Expiration date/time, it only sends name/value at this moment. and when Cookie is received by the server, it sets default to some 01/01/001 date.
0
 
LVL 17

Assisted Solution

by:Carlos Villegas
Carlos Villegas earned 1776 total points
ID: 35516476
In a simple way, Yes, because ASPNET just know the value of that cookie, nothing else.
0
 

Author Comment

by:TheCommunicator
ID: 35700419
Thanks :).
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article was originally published on Monitis Blog, you can check it here . Today it’s fairly well known that high-performing websites and applications bring in more visitors, higher SEO, and ultimately more sales. By the same token, downtime…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question