【问题标题】:How to clear browser cache when user log off in asp.net using c#?当用户使用 c# 在 asp.net 中注销时如何清除浏览器缓存?
【发布时间】:2014-03-10 16:40:44
【问题描述】:

作为 asp.net 中的新功能。在我的log offon click event 成员资格的asp.net 应用程序中,使用功能ClearSession(),但是如果我在浏览器上单击后退按钮,它会转发到缓存页面,注销后会出现问题。如何清除浏览器中的缓存,以便用户在未登录时无法查看其个人资料

protected void ClearSession()
{
    FormsAuthentication.SignOut();
    Session.Clear();
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ExpiresAbsolute = DateTime.UtcNow.AddDays(-1d);
    Response.Expires = -1500;
    Response.CacheControl = "no-Cache";
}

【问题讨论】:

标签: c# asp.net cache-control


【解决方案1】:

我想你快到了。您需要更多的 HTML 标头来支持所有浏览器。根据this article on SO,这些是适用于所有浏览器的:

Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

完整的代码是:

HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
HttpContext.Current.Response.AddHeader("Expires", "0");

【讨论】:

    【解决方案2】:
    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate");
    response.setHeader("Pragma", "no-cache"); 
    response.setDateHeader("Expires", 0); 
    

    这些标题仅适用于包含它们的页面,而不适用于所有 Web 应用程序,因此您应该将包含此标题的过滤器添加到所有页面,或者您可以禁用后退按钮。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 2010-10-10
      相关资源
      最近更新 更多