Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Expiring cookies

    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 Guest

  2. Similar Questions and Discussions

    1. Expiring Page After transaction
      Hi Guys, Need your ideas how to do this. I want to expired the page after transaction if user hit the back button. How do I trigger that it is...
    2. expiring sessions
      I've seen a couple of questions regarding the expiring of sessions in PHP. The way to control how sessions expire is to set some options in...
    3. FormsAuth Ticket Keeps Expiring
      Calling out ASP.NET Forms Auth Experts! I need your help. Using FormsAuthentication to auth against Active Directory. During my login process,...
    4. Cookies expiring erratically
      "Brent" <passing@designs-unlimited.com> wrote in message news:05d001c37109$fc5fb180$a401280a@phx.gbl... Don't know about that. Is your cookie...
    5. expiring a page
      Hi 1)Rather than disable the back button you can use the location.replace javascript method on the pages that you dont want people to be able to...
  3. #2

    Default Re: Expiring cookies

    "Jim Kelly" <cryptojim@yahoo.com> wrote in
    news:-zednawFyfHWSbeiU-KYuQ@comcast.com:
    > 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:

    [url]http://www.codeproject.com/aspnet/AspNetCookies.asp[/url]


    Hope this helps.

    Chris.
    -------------
    C.R. Timmons Consulting, Inc.
    [url]http://www.crtimmonsinc.com/[/url]
    Chris R. Timmons Guest

  4. #3

    Default 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...
    > "Jim Kelly" <cryptojim@yahoo.com> wrote in
    > news:-zednawFyfHWSbeiU-KYuQ@comcast.com:
    >
    > > 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:
    >
    > [url]http://www.codeproject.com/aspnet/AspNetCookies.asp[/url]
    >
    >
    > Hope this helps.
    >
    > Chris.
    > -------------
    > C.R. Timmons Consulting, Inc.
    > [url]http://www.crtimmonsinc.com/[/url]

    Jim Kelly Guest

  5. #4

    Default 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:
    >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" <cryptojim@yahoo.com> wrote in
    >> news:-zednawFyfHWSbeiU-KYuQ@comcast.com:
    >>
    >> > 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:
    >>
    >> [url]http://www.codeproject.com/aspnet/AspNetCookies.asp[/url]
    >>
    >>
    >> Hope this helps.
    >>
    >> Chris.
    >> -------------
    >> C.R. Timmons Consulting, Inc.
    >> [url]http://www.crtimmonsinc.com/[/url]
    >
    someone Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139