对于我们的项目,我们的客户在ASP.NET Webforms 4.0应用程序中进行了“笔测试”,并发现了许多他们希望我们修复的安全问题。
到目前为止引起讨论最多的是发现应用程序允许缓存页面和内容,这可能会导致未经授权的用户看到他们看不到的数据(这就是“笔测试”发现的内容)粗略地说。
建议的“修复”是将cache-control
和pragma
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页面被高速缓存,但仍然保持静态内容的缓存?如果可能的话:我必须设置哪些标题来实现这一目标?
谢谢!
答案 0 :(得分:3)
如果您正在使用网站的母版页或扩展了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