Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I remove 'no-cache="Set-Cookie"' when I add a cookie to a HttpResponse?

I'm currently returning a cookie from a web service with code like this:

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

This results in the automatic addition of a no-cache directive in my Cache-Control header:

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

My client happens to handle this directive by straight up not caching the response at all. If I manually remove the no-cache directive before it hits the client, caching works great.

How can I prevent .NET from automatically adding this directive to responses containing cookies?

like image 223
ladenedge Avatar asked Dec 22 '12 18:12

ladenedge


1 Answers

HttpResponse determines whether it should add this directive based on whether the Cookies collection is non-empty. Therefore, if you add the header manually you can hide its presence from .NET:

response.AddHeader("Set-Cookie", String.Format(
        "{0}={1}; expires={2}; path=/; secure; HttpOnly",
        cookieName, cookieValue, expiresDate.ToString("R")));
like image 71
ladenedge Avatar answered Sep 28 '22 15:09

ladenedge