设置cookie的在会话结束时到期? asp.net [英] set cookie to expire at end of session? asp.net

查看:120
本文介绍了设置cookie的在会话结束时到期? asp.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶,我不能找到任何答案。

I'm surprised i couldnt find any answers.

如何设置我的SessionID在我的cookie在会议结束时到期? (当浏览器关闭或用户已经闲置一段领带)。

How do i set my sessionid in my cookie to expire at the end of session? (when the browser closes or the user has been inactive for a period of tie).

这两种解决方案,我发现是

The two solutions i found were

(httpcookie).Expires = HttpContext.Current.Session.Timeout

所以我不知道,如果用户在发布前检查了他的code这给了我一个编译错误。另一个是对到期日设置为1天前这我的直觉说是错误的。我该怎么做呢?

Which gave me a compile error so i dont know if the user checked his code before posting. And the other was to set the expire date to 1 day ago which my gut says is wrong. How do i do this?

推荐答案

您在谈论一个非持久性cookie。默认情况下asp.net以这种方式发送的cookie。它们之间的主要区别是一个持久的cookie有一个过期设置的值。

You're talking about a non-persistent cookie. By default asp.net sends cookies in that way. The main difference between them are that a persistent cookie has an expires value set.

所以,如果你不想cookie来坚持,那么就不要设置到期值。

So, if you don't want the cookie to persist, then do not set the expires value.

这表示,cookie将保留在内存中,直到浏览器实际上被关闭。比方说,他们浏览到您的网站,设置非持久性cookie。他们做的事情,并浏览了。后来他们使用相同的浏览器实例,回到你的网站。 cookie将依然存在。

That said, the cookie will remain in memory until the browser is actually closed. Let's say they browse to your site and you set a non-persistent cookie. They do things and browse away. Later they, using the same browser instance, come back to your site. The cookie will still be there.

现在,如果他们在任何时候关闭浏览器,那么该cookie将被冲刷掉。

Now, if they closed the browser at any point, then the cookie would be flushed out.

的一点是,不要设置Expires头。尤其不能当会话日到期。会议的日期一般都在未来只有20分钟左右,但到期日期向前滚动的用户浏览您的网站。

Point is, don't set the expires header. Especially not to when the session date expires. Session dates are generally only 20 or so minutes in the future, but the expiration date rolls forward as the user browses through your site.

=====更新=====

===== update =====

我用下面的code来进行测试:

I used the following code for testing:

    protected void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack) {
            HttpCookie c = Request.Cookies["test"];
            if (c != null) {
                Response.Write(String.Format("test value is {0} <br />", c.Value));
            }
        } else {
            HttpCookie c = new HttpCookie("test");
            c.Value = "HERE IT IS";
            Response.Cookies.Add(c);
        }
    }

    protected void Button1_Click(object sender, EventArgs e) {
        Response.Write("clicked<br />");
    }

在.aspx文件简单有一个按钮,发射了的button1_Click处理程序。当我使用任何最新的浏览器最初浏览到它(IE,火狐,铬),没有饼干。我点击后设置cookie的按钮。然后,我完全关闭浏览器,重新打开和浏览,回网站。在所有情况下的cookie就消失了。

the .aspx file simple had a button which fired that button1_click handler. When I initially browse to it using any of the latest browsers (ie, firefox, chrome) there is no cookie. After I click the button a cookie is set. Then I closed the browser completely, reopened and browsed back to the site. In all cases the cookie was gone.

这篇关于设置cookie的在会话结束时到期? asp.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆