当用户关闭他们的浏览器时,我希望我的cookie消失 – 我已经设置了一些看起来很有前途的属性,但是即使在关闭整个浏览器之后我的cookie也会恢复生存.
HttpCookie cookie = new HttpCookie("mycookie","abc"); cookie.HttpOnly = true; //Seems to only affect script access cookie.Secure = true; //Seems to affect only https transport
解决方法
这个问题已在网上发布1000次.在浏览器打开的情况下处理非持久性cookie超时的最佳方法是为超时添加一个键值.以下代码用于登录用户ID密钥值和加密(不包括)安全性以实现浏览器兼容性.我不使用表单身份验证.
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 }
我发现使用表单身份验证和Google Chrome的兼容性问题.