简体   繁体   English

如何在 C# 中创建非持久性(内存中)http cookie?

[英]How to create a non-persistent (in memory) http cookie in C#?

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.我希望我的 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

What property or method call am I missing to achieve an in memory cookie?我缺少什么属性或方法调用来实现内存中的 cookie?

cookie.Expires = DateTime.MinValue;

this cookie will expire, as soon as the browser is closed.一旦浏览器关闭,此 cookie 就会过期。

Cookies without an expiration explicitly set will automatically go away once the browsing session is over.浏览会话结束后,没有明确设置过期时间的 Cookie 将自动消失。

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.在您的测试中,请确保在重新打开以查找 cookie 之前关闭浏览器的每个实例。 If you continue to have problems post the browser name and revision.如果问题仍然存在,请发布浏览器名称和修订版。

If you do no set the Cookie.Expires property the cookie will be set to expire at the end of the browser session.如果您没有设置 Cookie.Expires 属性,cookie 将被设置为在浏览器会话结束时过期。

Cookie Will not be destroy on browser close if Taken from here Cookie 如果从此处获取,则不会在浏览器关闭时销毁

 HttpCookie cookie = new HttpCookie(name);
 cookie.Value = value;
 cookie.Expires = Convert.ToDateTime(“12/12/2008″);  //*difference is here*//
 Response.Cookies.Add(cookie);}

Cookie will be lost on browser close if Cookie 将在浏览器关闭时丢失,如果

     HttpCookie cookie = new HttpCookie(name);
     cookie.Value = value;
     Response.Cookies.Add(cookie);}

The best way to handle non-persistent cookies timeout with the browser open is add a key value for timeout.在浏览器打开的情况下处理非持久性 cookie 超时的最佳方法是为超时添加一个键值。 The code below is used for a log in user id key value and encryption(not included) security for browser compatibility.下面的代码用于登录用户 ID 密钥值和加密(不包括)浏览器兼容性的安全性。 I do not use forms authentication.我不使用表单身份验证。

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);

When checking the cookie key value use:检查 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
}

I have found compatibility issues using forms authentication and Google Chrome.我发现使用表单身份验证和 Google Chrome 存在兼容性问题。

Take a look at the ASP.NET Session variable.查看 ASP.NET Session变量。 This will persist depending upon your browser and can be set to be "cookieless" or with a hard timeout.这将根据您的浏览器持续存在,并且可以设置为“无 cookie”或硬超时。

http://msdn.microsoft.com/en-us/library/h6bb9cz9%28VS.71%29.aspx http://msdn.microsoft.com/en-us/library/h6bb9cz9%28VS.71%29.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM