Link to home
Start Free TrialLog in
Avatar of Smoerble
SmoerbleFlag for Germany

asked on

How to set Cookie.Expire time?

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;
                  this.Response.Cookies.Add(myCookie);
----
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:
http://www.csharphelp.com/archives/archive179.html

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

Thank you.
Avatar of mahanatti
mahanatti

Avatar of Smoerble

ASKER

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?
Update:
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?
SOLUTION
Avatar of NipNFriar_Tuck
NipNFriar_Tuck

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, as stated in
http://www.dotnet247.com/247reference/msgs/48/240266.aspx
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 ;)