如何在 C# 中创建非持久(在内存中)http cookie?
我希望我的 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
为了实现内存 cookie,我缺少什么属性或方法调用?
I want my cookie to disappear when the user closes their brower-- I've already set some promising looking properties, but my cookies pop back to live even after closing the entire browser.
HttpCookie cookie = new HttpCookie("mycookie", "abc");
cookie.HttpOnly = true; //Seems to only affect script access
cookie.Secure = true; //Seems to affect only https transport
What property or method call am I missing to achieve an in memory cookie?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
一旦浏览器关闭,该 cookie 就会过期。
this cookie will expire, as soon as the browser is closed.
浏览会话结束后,没有明确设置过期时间的 Cookie 将自动消失。
现在,“浏览会话”对于不同的浏览器有不同的含义。对于某些浏览器来说,这意味着浏览器的每个实例都已关闭。对于某些人来说,这仅意味着相关选项卡或原始浏览器已关闭。
在测试中,请确保在重新打开浏览器以查找 cookie 之前关闭浏览器的每个实例。如果您仍然遇到问题,请发布浏览器名称和版本。
Cookies without an expiration explicitly set will automatically go away once the browsing session is over.
Now, "browsing session" means different things to different browsers. For some browsers it means that every instance of the browser is closed. For some it just means that the relevant tabs or original browser is closed.
In your testing make sure you close EVERY instance of the browser before reopening to look for the cookie. If you continue to have problems post the browser name and revision.
如果您没有设置 Cookie.Expires 属性,则 cookie 将设置为在浏览器会话结束时过期。
If you do no set the Cookie.Expires property the cookie will be set to expire at the end of the browser session.
如果出现以下情况,Cookie 不会在浏览器关闭时被销毁 取自此处
如果出现以下情况,Cookie 将在浏览器关闭时丢失
Cookie Will not be destroy on browser close if Taken from here
Cookie will be lost on browser close if
在浏览器打开的情况下处理非持久 cookie 超时的最佳方法是添加超时键值。下面的代码用于登录用户 ID 键值和加密(不包括)安全性,以实现浏览器兼容性。我不使用表单身份验证。
检查 cookie 键值使用时:
我发现使用表单身份验证和 Google Chrome 存在兼容性问题。
The best way to handle non-persistent cookies timeout with the browser open is add a key value for timeout. The code below is used for a log in user id key value and encryption(not included) security for browser compatibility. I do not use forms authentication.
When checking the cookie key value use:
I have found compatibility issues using forms authentication and Google Chrome.
看一下 ASP.NET
Session
变量。这将持续存在,具体取决于您的浏览器,并且可以设置为“无 cookie”或硬超时。http://msdn.microsoft.com/en-us /library/h6bb9cz9%28VS.71%29.aspx
Take a look at the ASP.NET
Session
variable. This will persist depending upon your browser and can be set to be "cookieless" or with a hard timeout.http://msdn.microsoft.com/en-us/library/h6bb9cz9%28VS.71%29.aspx