I am trying to do some cookie management in an ASP.NET app that calls other web services. I am encountering an error that I can reproduce only in certain environments. My questions are:
- Is the difference between production and development enough to cause this issue?
- How could I figure out what is different between production/development?
- What can I do to work-around this issue?
Following are the details I use to reproduce the problem. The error that I see is:
Unhandled Exception: System.Net.CookieException:
An error occurred when parsing the Cookie header for Uri 'http://example.com/'.
---> System.Net.CookieException: Cookie format error.
at System.Net.CookieContainer.CookieCutter(Uri uri, String headerName,
String setCookieHeader, Boolean isThrow)
--- End of inner exception stack trace ---
at System.Net.CookieContainer.CookieCutter(Uri uri, String headerName,
String setCookieHeader, Boolean isThrow)
at System.Net.CookieContainer.SetCookies(Uri uri, String cookieHeader)
at Program.Main() in c:\Sample\Program.cs:line 21
I have created a console app that reproduces the problem (relevant parts below). Full code at https://compilr.com/adutton/cookiecutterexample/main.cs
string[] cultures = new[] { "en-US", "es-MX" };
const string cookieHeader = ".ASPXAUTH=SECURITYINFO; domain=.example.com; "
+ "expires=Mon, 06-Mar-2023 18:36:33 GMT; path=/; HttpOnly";
foreach (string culture in cultures)
{
Console.WriteLine("CookieCutting with culture: " + culture);
Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
CookieContainer ctr = new CookieContainer();
// The following line throws an exception
ctr.SetCookies(new Uri("http://example.com/"), cookieHeader);
}
This code works on my development machine (Windows 7, x64, .NET 4.5.50709) but not in production (Windows Server 2008 R2 Enterprise, x64, .NET 4.0.30319) where the code throws an exception for the es-MX
culture.
If I remove the date from the cookie header, the exception goes away, which leads me to believe that is a localization issue with the cookie parser. Perhaps this was fixed in .NET 4.0 -> 4.5?