cookie expiry date 01/01/0001

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Below is the code to save the cookie and set the expiry date of the cooki

Response.Cookies["demo"].Value ="Hello"
DateTime dt = DateTime.Now
TimeSpan ts = new TimeSpan(0,0,10,0)
Response.Cookies ["demo"].Expires = dt.Add(ts);

Below is the code to retrieve the stored cookie

if (!Page.IsPostBack)
HttpCookie dcookie = Request.Cookies["demo"]
if (dcookie != null)
urlevel.Text = dcookie.Values["level"]
urname.Text = dcookie.Values["user"];
daysx.Text = Convert.ToString(Request.Cookies["demo"].Expires)

els
Response.Redirect ("haha.htm")


When I display the Response.Cookies ["demo"].Expires = dt.Add(ts); it returns the current date + 10 minute
But the days.Text returns 01/01/0001. Why? Please help Thanks

Ra
 
Below is the code to save the cookie and set the expiry date of
the cookie

Response.Cookies["demo"].Value ="Hello";
DateTime dt = DateTime.Now;
TimeSpan ts = new TimeSpan(0,0,10,0);
Response.Cookies ["demo"].Expires = dt.Add(ts);

Below is the code to retrieve the stored cookie.

if (!Page.IsPostBack) {
HttpCookie dcookie = Request.Cookies["demo"];
if (dcookie != null) {
urlevel.Text = dcookie.Values["level"];
urname.Text = dcookie.Values["user"];
daysx.Text =
Convert.ToString(Request.Cookies["demo"].Expires);
}
else
Response.Redirect ("haha.htm");
}

When I display the Response.Cookies ["demo"].Expires =
dt.Add(ts); it returns the current date + 10 minutes But the
days.Text returns 01/01/0001. Why? Please help Thanks.

Ray,

http://www.codeproject.com/aspnet/AspNetCookies.asp


Chris.
 
HI, Chris

I had tried that
It still gives me 01/01/0001 when I displayed the expiry date from the cookie collection
 
HI, Chris:

I had tried that.
It still gives me 01/01/0001 when I displayed the expiry date
from the cookie collection

Ray,

Exactly. The browser will not send the expiration date of the cookie
back to the server (I don't know why). So trying to read
Request.Cookies["demo"].Expires will never work.

If you only need to know if the cookie has expired or not, the
browser takes care of that by not sending the cookie if it has
expired. However, if you need to know how much time is remaining
before the cookie expires, then you probably need to store another
expiration date in a cookie value so you can read it:

Response.Cookies["demo].Values["expiration_date"] = ...

and

daysx.Text = Convert.ToString(
Request.Cookies["demo"].Values["expiration_date"]);

Hope this helps.

Chris.
 
Back
Top