我认为,取过期时间,得到的始终为{1/1/1},这应该是ASP.NET的一个小BUG。
解决方法是:将过期时间作为一个值保存在cookie中,如果cookie存在再去取这个过期时间,而不是直接取cookie.Expires。但写入cookie时,还是要写Expires的,这样才能在客户端正确地删除cookie。
写入的代码:
private void Button2_Click(object sender, System.EventArgs e)
{
HttpCookie cookie = new HttpCookie("log");
cookie.Values.Add("name", "me");
cookie.Values.Add("pwd", "123");
cookie.Values.Add("Expires", DateTime.MaxValue.ToString());
cookie.Expires = System.DateTime.MaxValue;
Response.Cookies.Add(cookie);
}
读取时,
HttpCookie cookie = Request.Cookies["log"];
if(cookie != null)
{
if(DateTime.Parse(cookie.Values["Expires"]) > System.DateTime.Now)
{
//do something...
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。