【问题标题】:Cache-Control warning getting logged when add ResponseCache attribute添加 ResponseCache 属性时记录缓存控制警告
【发布时间】:2019-12-09 11:01:07
【问题描述】:

我有 ASP.NET Core 2.2 应用程序。当用户单击浏览器的后退按钮时,我在所有控制器上设置了no-store=true

[ResponseCache(NoStore = true)]
public class MyController:Controller
{
}

在 startup.cs 我添加 AutoValidateAntiforgeryTokenAttribute

services.AddMvc(options =>
            {   
                options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());                

            })

但是对于每个请求,我都会看到一条警告被记录

“Cache-Control”和“Pragma”标头已被覆盖并设置 分别设置为“no-cache, no-store”和“no-cache”以防止缓存 这个回应。任何使用防伪的响应都不应该是 缓存。

我知道任何使用防伪的响应都不应该被缓存,我确实设置了NoStore=true。那么为什么会有警告。

如果我删除 [ResponseCache(NoStore = true)],那么警告就消失了。但是,在用户单击后退按钮的情况下,我需要该属性

【问题讨论】:

    标签: asp.net-core asp.net-core-mvc asp.net-core-2.0


    【解决方案1】:

    NoStore 还不够。该警告是默认防伪逻辑检查“无缓存”的“缓存控制”和“杂注”标头值的结果。如果 'cache-control' 标头不包含 'no-cache' 或 'pragma' 标头不是 'no-cache' 或为空,则会创建该警告。

    您始终必须在缓存设置中包含该位置:

    [ResponseCache(Location = ResponseCacheLocation.None, NoStore = true)]
    

    这会将 'cache-control' 标头设置为 'no-store,no-cache' 并将 'pragma' 标头设置为 'no-cache' 并且不会创建警告。

    【讨论】:

      猜你喜欢
      • 2017-03-15
      • 2020-02-01
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-04
      • 2016-05-23
      • 1970-01-01
      相关资源
      最近更新 更多