.NET CookieException "Cookie format error" when thread culture is not English - only in certain environments

StackOverflow https://stackoverflow.com/questions/15257004

Question

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:

  1. Is the difference between production and development enough to cause this issue?
  2. How could I figure out what is different between production/development?
  3. 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?

Was it helpful?

Solution 2

As @nunzabar pointed out, the problem is way the current culture sets a comma in the day-of-week. In this case, installing the .NET 4.5 framework caused the problem to disappear. I did not decompile the code to see the differences between .NET 4.0 and 4.5 but it was fixed when we installed the new version of the framework.

OTHER TIPS

I believe the problem is with the comma after the day-of-week, as seen here: Help With .NET CookieContainer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top