2017-05-01 41 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