Connecting Tech Pros Worldwide Help | Site Map

Expiring cookies

  #1  
Old November 17th, 2005, 07:47 PM
Jim Kelly
Guest
 
Posts: n/a
Hi,

I'm having a strange problem with setting a cookie's expiration date. The
relevant code is as follows:

HttpCookie hc = new HttpCookie("MyCookie");
hc.Values.Add("UserName", tbUserName.Text);
hc.Expires = DateTime.Now.AddDays(30);
Response.AppendCookie(hc);

As you can see it's just a simple cookie storing one value. But when I check
its expiration date (by writing it to an ASP:Label control on the ASPX
page), DateTime.MinValue (i.e., 1/1/0001 12:00:00 AM) is returned every
time. I've researched this extensively online and in MS's documentation and
found nothing addressing this problem very clearly. I have read that an
expiration date of DateTime.MinValue means the cookie will never expire. I
suspect it has something to do with the session cookie ASP.NET sends to the
client automatically (the one containing ASP.NET_SessionId); maybe I'm
inadvertantly obtaining that cookie's expiration date, which would be "never
expires" because it's just a session cookie, as opposed to my persistent
cookie. But the code I use to obtain the expiration date is
Request.Cookies["MyCookie"].Expires.ToString(), which seems to refer to my
cookie to me.

Does anyone have any ideas?


Thanks very much,
Jim Kelly


  #2  
Old November 17th, 2005, 07:48 PM
Chris R. Timmons
Guest
 
Posts: n/a

re: Expiring cookies


"Jim Kelly" <cryptojim@yahoo.com> wrote in
news:-zednawFyfHWSbeiU-KYuQ@comcast.com:
[color=blue]
> Hi,
>
> I'm having a strange problem with setting a cookie's expiration
> date. The relevant code is as follows:
>
> HttpCookie hc = new HttpCookie("MyCookie");
> hc.Values.Add("UserName", tbUserName.Text);
> hc.Expires = DateTime.Now.AddDays(30);
> Response.AppendCookie(hc);
>
> As you can see it's just a simple cookie storing one value. But
> when I check its expiration date (by writing it to an ASP:Label
> control on the ASPX page), DateTime.MinValue (i.e., 1/1/0001
> 12:00:00 AM) is returned every time. I've researched this
> extensively online and in MS's documentation and found nothing
> addressing this problem very clearly. I have read that an
> expiration date of DateTime.MinValue means the cookie will never
> expire. I suspect it has something to do with the session cookie
> ASP.NET sends to the client automatically (the one containing
> ASP.NET_SessionId); maybe I'm inadvertantly obtaining that
> cookie's expiration date, which would be "never expires" because
> it's just a session cookie, as opposed to my persistent cookie.
> But the code I use to obtain the expiration date is
> Request.Cookies["MyCookie"].Expires.ToString(), which seems to
> refer to my cookie to me.
>
> Does anyone have any ideas?[/color]

Jim,

The browser never sends the cookie's expiration date back to the
server. This isn't generally a problem, since the browser won't send
the cookie to the server if the cookie has expired.

Here's an excellent article about cookies and .Net:

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


Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
  #3  
Old November 17th, 2005, 07:49 PM
Jim Kelly
Guest
 
Posts: n/a

re: Expiring cookies


Chris,

Thanks. It figures that the one article that answers my question in one
sentence is the one that didn't turn up in my search!


Jim

"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
news:Xns93CB3E990F88Fcrtimmonscrtimmonsin@207.46.2 48.16...[color=blue]
> "Jim Kelly" <cryptojim@yahoo.com> wrote in
> news:-zednawFyfHWSbeiU-KYuQ@comcast.com:
>[color=green]
> > Hi,
> >
> > I'm having a strange problem with setting a cookie's expiration
> > date. The relevant code is as follows:
> >
> > HttpCookie hc = new HttpCookie("MyCookie");
> > hc.Values.Add("UserName", tbUserName.Text);
> > hc.Expires = DateTime.Now.AddDays(30);
> > Response.AppendCookie(hc);
> >
> > As you can see it's just a simple cookie storing one value. But
> > when I check its expiration date (by writing it to an ASP:Label
> > control on the ASPX page), DateTime.MinValue (i.e., 1/1/0001
> > 12:00:00 AM) is returned every time. I've researched this
> > extensively online and in MS's documentation and found nothing
> > addressing this problem very clearly. I have read that an
> > expiration date of DateTime.MinValue means the cookie will never
> > expire. I suspect it has something to do with the session cookie
> > ASP.NET sends to the client automatically (the one containing
> > ASP.NET_SessionId); maybe I'm inadvertantly obtaining that
> > cookie's expiration date, which would be "never expires" because
> > it's just a session cookie, as opposed to my persistent cookie.
> > But the code I use to obtain the expiration date is
> > Request.Cookies["MyCookie"].Expires.ToString(), which seems to
> > refer to my cookie to me.
> >
> > Does anyone have any ideas?[/color]
>
> Jim,
>
> The browser never sends the cookie's expiration date back to the
> server. This isn't generally a problem, since the browser won't send
> the cookie to the server if the cookie has expired.
>
> Here's an excellent article about cookies and .Net:
>
> http://www.codeproject.com/aspnet/AspNetCookies.asp
>
>
> Hope this helps.
>
> Chris.
> -------------
> C.R. Timmons Consulting, Inc.
> http://www.crtimmonsinc.com/[/color]


  #4  
Old November 17th, 2005, 07:55 PM
someone
Guest
 
Posts: n/a

re: Expiring cookies


Jim,
I am running into the same problem and did read the article mentioned
below. Still don't see what is going on. The code I am using is:
BECartCookie = new HttpCookie("BECart");
BECartCookie.Value = CartID.ToString();
BECartCookie.Path = "/";
BECartCookie.Expires = dNow.AddHours(48);
Response.Cookies.Set(BECartCookie);
Is there anything wrong with this? Every time I look at the cookie it
returns the "beginning of time" expiration date.
Thanks,
Mike

On Sat, 2 Aug 2003 08:51:54 -0600, "Jim Kelly" <cryptojim@yahoo.com>
wrote:
[color=blue]
>Chris,
>
>Thanks. It figures that the one article that answers my question in one
>sentence is the one that didn't turn up in my search!
>
>
>Jim
>
>"Chris R. Timmons" <crtimmons@X_NOSPAM_Xcrtimmonsinc.com> wrote in message
>news:Xns93CB3E990F88Fcrtimmonscrtimmonsin@207.46. 248.16...[color=green]
>> "Jim Kelly" <cryptojim@yahoo.com> wrote in
>> news:-zednawFyfHWSbeiU-KYuQ@comcast.com:
>>[color=darkred]
>> > Hi,
>> >
>> > I'm having a strange problem with setting a cookie's expiration
>> > date. The relevant code is as follows:
>> >
>> > HttpCookie hc = new HttpCookie("MyCookie");
>> > hc.Values.Add("UserName", tbUserName.Text);
>> > hc.Expires = DateTime.Now.AddDays(30);
>> > Response.AppendCookie(hc);
>> >
>> > As you can see it's just a simple cookie storing one value. But
>> > when I check its expiration date (by writing it to an ASP:Label
>> > control on the ASPX page), DateTime.MinValue (i.e., 1/1/0001
>> > 12:00:00 AM) is returned every time. I've researched this
>> > extensively online and in MS's documentation and found nothing
>> > addressing this problem very clearly. I have read that an
>> > expiration date of DateTime.MinValue means the cookie will never
>> > expire. I suspect it has something to do with the session cookie
>> > ASP.NET sends to the client automatically (the one containing
>> > ASP.NET_SessionId); maybe I'm inadvertantly obtaining that
>> > cookie's expiration date, which would be "never expires" because
>> > it's just a session cookie, as opposed to my persistent cookie.
>> > But the code I use to obtain the expiration date is
>> > Request.Cookies["MyCookie"].Expires.ToString(), which seems to
>> > refer to my cookie to me.
>> >
>> > Does anyone have any ideas?[/color]
>>
>> Jim,
>>
>> The browser never sends the cookie's expiration date back to the
>> server. This isn't generally a problem, since the browser won't send
>> the cookie to the server if the cookie has expired.
>>
>> Here's an excellent article about cookies and .Net:
>>
>> http://www.codeproject.com/aspnet/AspNetCookies.asp
>>
>>
>> Hope this helps.
>>
>> Chris.
>> -------------
>> C.R. Timmons Consulting, Inc.
>> http://www.crtimmonsinc.com/[/color]
>[/color]

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
New to Javascript, help with expiring cookies chrisx answers 1 May 28th, 2007 05:23 AM
Forms Authentication Cookies Never Expire Joey Powell answers 3 November 18th, 2005 01:08 AM
Writing cookies from include file Brian Burgess answers 20 July 19th, 2005 08:22 AM
Sessions and expiring cookies Derek Fountain answers 1 July 17th, 2005 11:44 AM