Expiration date seems not to work

  • Thread starter Frederic Gignac
  • Start date
F

Frederic Gignac

Hiya all,

My buddy and I got a bug with the expiration date of a cookie. When we
want to look out the expiration date, the only thing we've got, it's
01/01/0001.

As we know, when we want to create a persitant cookie, we have to put
a expiration date, otherwise, it will be a session cookie. So here our
code :

HttpCookie cookie = new HttpCookie(m_propertyID);
cookie.Value = m_property;
if (m_temporaryCookie == false)
{
cookie.Expires = DateTime.MaxValue;
}
HttpContext.Current.Response.Cookies.Add(cookie);

And when we want to see the value :

HttpCookie cookie =
HttpContext.Current.Request.Cookies[m_propertyID];

if ( cookie != null)
{
HttpContext.Current.Response.Write(cookie.Expires);
}


With that, we've always got the 01/01/0001. If you have any ideas to
help us, let us know.

Btw, sorry for the english typos :)
 
C

Chris R. Timmons

(e-mail address removed) (Frederic Gignac) wrote in
Thank you Chris for the link. Besides I've got a 500 error page,
the Google cache saves me ;)

After reading it, I'm now aware that we can't do what we will
attempt to.

We want to know if the cookie is non-persistant or a persistant
one. So we think to look for the expires value of the cookie to
let us know if it's a non or a persistant one.

Obviously, this isn't the good method after reading the
article...

Any suggests to make the difference between the two types of
cookies ? Is there a method or something that we haven't see yet
?

One way to tell the difference would be to set a value in the cookie:

// Persistent cookie.
HttpCookie cookie = new HttpCookie("MyCookie");
cookie.Expires = DateTime.AddDays(10);
cookie.Values["Persistent"] = "true";
cookie.Values["Data"] = "Hello, world!";
HttpContext.Current.Response.Cookies.Add(cookie);

or

// Non-persistent cookie because no expiraton date is set.
// Will be destroyed when the user closes their browser.
HttpCookie cookie = new HttpCookie("MyCookie");
cookie.Values["Persistent"] = "false";
cookie.Values["Data"] = "foo";
HttpContext.Current.Response.Cookies.Add(cookie);

You can use code like this to see if the cookie is persistent or not:

HttpCookieCollection cookies = HttpContext.Current.Request.Cookies;
bool isMyCookiePersistent = false;

if (cookies["MyCookie"] != null)
{
isMyCookiePersistent =
(cookies["MyCookie"].Values["Persistent"] == "true");
}


Hope this helps.

Chris.
 
F

Frederic Gignac

Chris R. Timmons said:
One way to tell the difference would be to set a value in the cookie:

// Persistent cookie.
HttpCookie cookie = new HttpCookie("MyCookie");
cookie.Expires = DateTime.AddDays(10);
cookie.Values["Persistent"] = "true";
cookie.Values["Data"] = "Hello, world!";
HttpContext.Current.Response.Cookies.Add(cookie);

or

// Non-persistent cookie because no expiraton date is set.
// Will be destroyed when the user closes their browser.
HttpCookie cookie = new HttpCookie("MyCookie");
cookie.Values["Persistent"] = "false";
cookie.Values["Data"] = "foo";
HttpContext.Current.Response.Cookies.Add(cookie);

You can use code like this to see if the cookie is persistent or not:

HttpCookieCollection cookies = HttpContext.Current.Request.Cookies;
bool isMyCookiePersistent = false;

if (cookies["MyCookie"] != null)
{
isMyCookiePersistent =
(cookies["MyCookie"].Values["Persistent"] == "true");
}


Hope this helps.

Chris.


Thanks again ! It was this solution that we've been thinking last
week, but since a module administrate the cookie, we will have to
modify a hell lot of code. Since we have to give our project within
days, we won't have the time to do all these modifications. For sure,
for next projects, we'll change our module.

Thank you anyway :)
Fred
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,135
Latest member
VeronaShap
Top