J
Jim Kelly
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
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