cookie没有在c#中过期

时间:2014-01-04 08:44:47

标签: c# asp.net cookies

我的cookies没有过期,

2pages 1page generating cookiesexpire cookies,第二页我正在尝试HttpCookie editorid = new HttpCookie("editorid"); editorid.Value = Convert.ToString(rr1.First().editor_id); Response.Cookies.Add(editorid); Response.Redirect("2page.aspx"); ,然后将其重定向到生成但后面生成的页面点击浏览器该页面打开

.cs代码 生成的位置在 1page.aspx

protected void Page_Load(object sender, EventArgs e)
{
     if (Request.Cookies["editorid"] != null)
    {           
    }
     else
    {
        Response.Redirect("1page.aspx");
    }
}
 protected void LinkButton1_Click(object sender, EventArgs e)
{
    Response.Cookies["editorid"].Expires = DateTime.Now.AddSeconds(-1);
    Response.Redirect("1page.aspx");
}

.cs代码重定向并获得已过期”的位置在 2page.aspx

1page.aspx

点击 Linkbutton1 后,它会被重定向到2page.aspx,但当我点击后退按钮时,会再次返回{{1}}

我应该如何使饼干过期?

2 个答案:

答案 0 :(得分:3)

从您的描述中看来,当用户点击后退按钮时,页面会从后退按钮缓存中拉出来,这非常具有侵略性。

您可以尝试通过changing the Cache-Control header阻止此缓存,在ASP.NET中,可以使用页面顶部的OutputCache指令来完成此操作。

<%@ OutputCache Location="None" VaryByParam="None" NoStore="true" Duration="1" %>

答案 1 :(得分:1)

使用会话,这样可以获得更多控制权,并且可以将会话设置为null(如果不再使用)