当我向HttpResponse添加cookie时,如何删除'no-cache ="Set-Cookie"'?

lad*_*dge 2 .net asp.net cookies wcf

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

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

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

Cache-Control:public,no-cache ="Set-Cookie",必须重新验证,max-age = 60

我的客户端碰巧直接处理这个指令而根本没有缓存响应.如果我在命中客户端之前手动删除no-cache指令,缓存效果很好.

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

lad*_*dge 6

HttpResponse确定是否应根据Cookies集合是否为空来添加此指令.因此,如果手动添加标头,则可以隐藏其在.NET中的存在:

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