Expiring cookies

Discussion in 'ASP .Net' started by Jim Kelly, Aug 1, 2003.

  1. Jim Kelly

    Jim Kelly Guest

    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
     
    Jim Kelly, Aug 1, 2003
    #1
    1. Advertising

  2. "Jim Kelly" <> wrote in
    news::

    > 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?


    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/
     
    Chris R. Timmons, Aug 2, 2003
    #2
    1. Advertising

  3. Jim Kelly

    Jim Kelly Guest

    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...
    > "Jim Kelly" <> wrote in
    > news::
    >
    > > 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?

    >
    > 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/
     
    Jim Kelly, Aug 2, 2003
    #3
  4. Jim Kelly

    someone Guest

    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" <>
    wrote:

    >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...
    >> "Jim Kelly" <> wrote in
    >> news::
    >>
    >> > 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?

    >>
    >> 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/

    >
     
    someone, Aug 5, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Natty Gur

    Re: expiring a page

    Natty Gur, Jul 2, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    422
    Natty Gur
    Jul 2, 2003
  2. Replies:
    2
    Views:
    3,947
  3. Omer
    Replies:
    5
    Views:
    321
    Juan T. Llibre
    Dec 8, 2006
  4. _Who
    Replies:
    7
    Views:
    2,686
  5. Steve

    Cookies expiring when user logs out?

    Steve, Jul 5, 2007, in forum: ASP .Net Security
    Replies:
    6
    Views:
    152
    Steve
    Jul 6, 2007
Loading...

Share This Page