这个问题已经在网上公布了1000+次。在浏览器打开的情况下处理非永久cookie超时的最佳方式是为超时添加一个键值。下面的代码用于登录用户标识键值和用于浏览器兼容性的加密(未包括)安全性。我不使用表单身份验证。
HttpCookie cookie = new HttpCookie(name);
cookie.Values["key1"] = value;
cookie.Values["key2"] = DateTime.Now.AddMinutes(70).ToString();
//timeout 70 minutes with browser open
cookie.Expires = DateTime.MinValue;
cookie.Domain = ConfigurationManager.AppSettings["website_domain"];
System.Web.HttpContext.Current.Response.Cookies.Add(cookie);
当检查cookie的键值使用:
try
{
DateTime dateExpireDateTime;
dateExpireDateTime = DateTime.Parse(HttpContext.Current.Request.Cookies[name]["key2"]);
if (DateTime.Now > dateExpireDateTime)
{
//cookie key value timeout code
}
else
{
//reset cookie
}
catch
{
//clear cookie and redirect to log in page
}
我发现使用窗体身份验证和谷歌Chrome兼容性问题。
这是不是真的与我的问题丢失。想象一下,如果会话被禁用,并且我仍然想创建一个Http Cookie,当浏览器关闭/不写入磁盘等时,它将消失。我认为其他一些答案在正确的轨道上。 – MatthewMartin 2010-12-21 16:15:02