How to set Cookie.Expire time?

Posted on 2004-08-05
Last Modified: 2012-08-13
Hoi all...
I have trouble with the following code:
                  DateTime dtNow = DateTime.Now;
                  TimeSpan tsMinute = new TimeSpan(365, 0, 0, 0);
                  HttpCookie myCookie = new HttpCookie(cookieName);
                  myCookie.Expires = dtNow + tsMinute;
                  myCookie["memberID"] = iMemberID.ToString();
                  myCookie["memberNick"] = sMemberNick;
                  myCookie["memberPassword"] = sMemberPassword;
Now I check, what the expire time for the cookie is:
                  myCookie = inPage.Request.Cookies[cookieName];
                  Response.Write("this cookie expires: "+myCookie.Expires);
I always get:
01.01.0001 00:00:00

The idea how to set the cookie is taken from here:

Can someone tell me how I tell my cookie to life for 365 days from now?

Thank you.
Question by:Smoerble
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 11725181

Author Comment

ID: 11725261
This contains
DateTime dtExpiry = DateTime.Now.AddDays(iDaysToExpire);
Response.Cookies[cookiename].Expires =dtExpiry;
I did that too, same result as above: expire time seems not to be correct.
Any other idea?

Author Comment

ID: 11725364
I checked what the browser says about the cookie:
the date is set correctly... so the question is, why doesn't it display the correct date in my code?
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Assisted Solution

NipNFriar_Tuck earned 150 total points
ID: 11727243
Hmmm... I am wondering if you are cheking your cookie after another page hit?  If you are checking your cookie after setting it you are looking at the wrong object.  You set your cookie on Response and check on Request.  Also, if cookies are turned off on the browser then this may happen.

Accepted Solution

s_sansanwal earned 350 total points
ID: 11732548

Author Comment

ID: 11738452
Ok, as stated in
the cookie does not send back the correct dateTime when using ....Expires.
Thank you.

If anybody has any other idea, please feel free to share ;)

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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