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

发布于 2024-10-08 07:49:53 字数 318 浏览 0 评论 0原文

我希望我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

烟燃烟灭 2024-10-15 07:49:53
cookie.Expires = DateTime.MinValue;

一旦浏览器关闭,该 cookie 就会过期。

cookie.Expires = DateTime.MinValue;

this cookie will expire, as soon as the browser is closed.

聊慰 2024-10-15 07:49:53

浏览会话结束后,没有明确设置过期时间的 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.

毁虫ゝ 2024-10-15 07:49:53

如果您没有设置 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.

浅笑依然 2024-10-15 07:49:53

如果出现以下情况,Cookie 不会在浏览器关闭时被销毁 取自此处

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

如果出现以下情况,Cookie 将在浏览器关闭时丢失

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

Cookie Will not be destroy on browser close if Taken from here

 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

     HttpCookie cookie = new HttpCookie(name);
     cookie.Value = value;
     Response.Cookies.Add(cookie);}
笑着哭最痛 2024-10-15 07:49:53

在浏览器打开的情况下处理非持久 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 存在兼容性问题。

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.

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:

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.

弥繁 2024-10-15 07:49:53

看一下 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文