【问题标题】:How can I remove 'no-cache="Set-Cookie"' when I add a cookie to a HttpResponse?当我将 cookie 添加到 HttpResponse 时,如何删除“no-cache =“Set-Cookie”?
【发布时间】:2012-12-09 22:15:31
【问题描述】:

我目前正在从 Web 服务返回一个 cookie,代码如下:

HttpResponse response = ...;
var cookie = new HttpCookie(cookieName)
{
    Value = cookieValue,
    Expires = expiresDate,
    HttpOnly = true,
    Path = "/",
    Secure = true,
};
response.Cookies.Add(cookie);

这会导致在我的 Cache-Control 标头中自动添加 no-cache 指令:

Cache-Control: public, no-cache="Set-Cookie", must-revalidate, max-age=60

我的客户恰好通过直接不缓存响应来处理此指令。如果我在 no-cache 指令到达客户端之前手动删除它,缓存效果很好。

如何防止 .NET 自动将此指令添加到包含 cookie 的响应中?

【问题讨论】:

    标签: asp.net .net wcf cookies


    【解决方案1】:

    HttpResponse 根据Cookies 集合是否为非空来决定是否添加该指令。因此,如果您手动添加标头,您可以在 .NET 中隐藏它的存在:

    response.AddHeader("Set-Cookie", String.Format(
            "{0}={1}; expires={2}; path=/; secure; HttpOnly",
            cookieName, cookieValue, expiresDate.ToString("R")));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 2020-02-14
      • 2012-06-25
      • 2016-11-23
      • 2011-03-25
      • 2011-09-26
      相关资源
      最近更新 更多