65.9K
CodeProject 正在变化。 阅读更多。
Home

在 ASP.NET 中防止浏览器缓存网页,适用于所有浏览器 (IE/Firefox..)

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (1投票)

2013 年 10 月 11 日

CPOL

1分钟阅读

viewsIcon

30069

在本文中,我将解释如何在 ASP.NET 中防止浏览器缓存网页。 这几乎是每个开发者都会遇到的一个最大问题。

在本文中,我将解释如何在 ASP.NET 中防止浏览器缓存网页。 这几乎是每个开发者都会遇到的一个最大问题。

为什么浏览器会缓存?
  为了加快用户在网络上的体验,大多数浏览器都实现了一种称为缓存的技术。 缓存允许将网页、图像等信息保存在用户的计算机上。 如果用户请求之前请求过的网页,浏览器可以通过从缓存中检索信息来更快地访问信息,而不是再次向站点本身发出请求。

  一方面,这是一种优势,但当您显示敏感信息时,这将是一个很大的缺点。 最近,我们在当前项目中发现了一个问题,用户登录后执行一些操作,然后注销。 如果用户点击后退按钮,它仍然会显示信息,就好像用户仍然登录一样。 唔…… 我们尝试了不同的方法来处理这个问题。 但我们在 Firefox 上遇到了问题。

   所以我决定在母版页的加载事件中编写逻辑。 并且我在注销页面添加了一些登录逻辑。 这是代码。

将此代码放置在母版页的加载事件中

HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(false);  
            HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);  
            HttpContext.Current.Response.Cache.SetNoStore();
            Response.Cache.SetExpires(DateTime.Now.AddSeconds(60));
            Response.Cache.SetValidUntilExpires(true);

 

         In Logout page Load   add this code  
  


Response.AddHeader("Pragma", "no-cache");
            Response.CacheControl = "no-cache";
            Response.Cache.SetAllowResponseInBrowserHistory(false);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            Response.Expires = -1;
            Session.Abandon();
            ClientScript.RegisterClientScriptBlock(this.GetType(),"signout", "DisableHistory()", true );


  
write this code in logout mark up page






function DisableHistory() {
 
           window.history.forward(1);
       }
       function RedirectToHome() {
 
           setTimeout("window.location = 'Index.aspx'",0);
       }
     
   </script>
 

在注销页面的 body onload 中调用此 RedirectToHome 方法




<body onload ="RedirectToHome();">
 
Run the application.Have a fun …
© . All rights reserved.