【问题标题】:ASP.NET Webforms - turn off caching, but only for "pages", no for static contentASP.NET Webforms - 关闭缓存,但仅适用于“页面”,不适用静态内容
【发布时间】:2017-05-01 11:05:01
【问题描述】:

对于我们的一个项目,我们的客户对 ASP.NET Webforms 4.0 应用程序进行了“笔测试”,发现了许多他们希望我们解决的安全问题。

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

建议的“修复”是将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 页面及其呈现的数据,但仍保留对静态内容的缓存?如果可能的话:我必须设置哪些标题才能实现这一目标?

谢谢!

【问题讨论】:

    标签: caching webforms asp.net-4.0 cache-control


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      • 1970-01-01
      • 2014-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多