I am getting a CookieException when adding a Cookie to a CookieContainer, like so:
ProxyRequest.CookieContainer = new CookieContainer();
Uri serverUri = new Uri(ServerURL);
foreach (string name in Context.Request.Cookies.AllKeys)
{
var requestCookie = Context.Request.Cookies[name];
ProxyRequest.CookieContainer.Add(new Cookie(requestCookie.Name, requestCookie.Value, "/", serverUri.DnsSafeHost));
}
The documentation for CookieException says that this happens when the cookie is greater than MaxCookieSize in the CookieContainer; however, MaxCookieSize is 4096 and this cookie is only 96 bytes long.
Is 4096 limit the limit for all the cookies in the container added together? Is there some other reason I am getting this exception?
The message with the exception is: The 'Value'='84-B2,PX,TT|15-TL|30-T6,TL|23-GA,TL,TT|29-MR|25-CX,GA,TT|31-CX,GA,PX|9-B2,TF,TR|7-B2,CX,PX,TL,TT' part of the cookie is invalid.
It may be possible that your domain has other cookies you can't store more than 20 cookies per site.
By using a single cookie with subkeys, you use fewer of those 20 cookies that your site is allotted.
In addition, a single cookie takes up about 50 characters for overhead (expiration information, and so on), plus the length of the value that you store in it, all of which counts toward the 4096-byte limit. If you store five subkeys instead of five separate cookies, you save the overhead of the separate cookies and can save around 200 bytes.