2017-05-01 43 views
0

对于我们的项目,我们的客户在ASP.NET Webforms 4.0应用程序中运行“笔测试”,发现一些他们希望我们解决的安全问题。ASP.NET Webforms - 关闭缓存,但仅限于“页面”,不支持静态内容

迄今为止引起最多讨论的一个发现是应用程序允许页面和内容被缓存,这可能会导致未经授权的用户看到他们不应该看到的数据(这就是“笔测试”发现说,大致)。

建议的“修复”是对cache-controlpragma HTTP标头设置为no-cache避免这种缓存,加入这个我web.config

<system.webServer> 
    <httpProtocol> 
     <customHeaders> 
      <add name="Cache-Control" value="no-cache, no-store, must-revalidate, private"/> 
      <add name="Pragma" value="no-cache"/> 
      <add name="Expires" value="-1"/> 
     </customHeaders> 
    </httpProtocol> 
</system.webServer> 

但我有点舍不得在全球范围内做到这一点 - 这是否也会关闭应用程序的图像,Javascript和CSS文件的缓存?这可能会对网站性能产生重大负面影响 - 不是吗?

那么我可以做些什么“之间”?阻止实际的ASP.NET页面被它们存在的数据缓存,但是仍然保持静态内容的缓存?如果可能的话:我必须设置什么标题来实现这个目标?

谢谢!

回答

1

如果您正在使用站点的母版页或已使用扩展的Page类扩展Page类和创建的页面,则可以将代码放入相应的Page_Load事件中。

Response.Cache.SetCacheability(HttpCacheability.NoCache); //Cache-Control : no-cache, Pragma : no-cache 
Response.Cache.SetExpires(DateTime.Now.AddDays(-1)); //Expires : date time 
Response.Cache.SetNoStore(); //Cache-Control : no-store 
Response.Cache.SetProxyMaxAge(new TimeSpan(0, 0, 0)); //Cache-Control: s-maxage=0 
Response.Cache.SetValidUntilExpires(false); 
Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);//Cache-Control: must-revalidate