Connecting Tech Pros Worldwide Help | Site Map

Expiration date seems not to work

Frederic Gignac
Guest
 
Posts: n/a
#1: Nov 17 '05
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 :)
Chris R. Timmons
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Expiration date seems not to work


fgignac@neomedia.com (Frederic Gignac) wrote in
news:8878eaae.0307070719.23f5f551@posting.google.c om:
[color=blue]
> 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
> ?[/color]

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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Frederic Gignac
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Expiration date seems not to work


"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message news:<Xns93B18E888DFABcrtimmonscrtimmonsin@207.46. 248.16>...[color=blue][color=green]
> > One way to tell the difference would be to set a value in the cookie:[/color]
>
> // 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.
> -------------
> C.R. Timmons Consulting, Inc.
> http://www.crtimmonsinc.com/[/color]


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
Closed Thread