How can I get all Cookies of a CookieContainer?

.net, c#, cookiecontainer

Solution

A solution using reflection:

public static CookieCollection GetAllCookies(CookieContainer cookieJar)
{
    CookieCollection cookieCollection = new CookieCollection();

    Hashtable table = (Hashtable) cookieJar.GetType().InvokeMember("m_domainTable",
                                                                    BindingFlags.NonPublic |
                                                                    BindingFlags.GetField |
                                                                    BindingFlags.Instance,
                                                                    null,
                                                                    cookieJar,
                                                                    new object[] {});

    foreach (var tableKey in table.Keys)
    {
        String str_tableKey = (string) tableKey;

        if (str_tableKey[0] == '.')
        {
            str_tableKey = str_tableKey.Substring(1);
        }

        SortedList list = (SortedList) table[tableKey].GetType().InvokeMember("m_list",
                                                                    BindingFlags.NonPublic |
                                                                    BindingFlags.GetField |
                                                                    BindingFlags.Instance,
                                                                    null,
                                                                    table[tableKey],
                                                                    new object[] { });

        foreach (var listKey in list.Keys)
        {
            String url = "https://" + str_tableKey + (string) listKey;
            cookieCollection.Add(cookieJar.GetCookies(new Uri(url)));
        }
    }

    return cookieCollection;
}

.NET 6 Update

Finally, .NET 6 was released and introduced the `CookieContainer.GetAllCookies()` method which extracts the `CookieCollection` - Documentation link.

public System.Net.CookieCollection GetAllCookies();

Problem

I want to export a CookieContainer to JSON using Newtonsoft.Json but unfortunately CookieContainer hasn't an enumerator or stuff, so I can't cycle through it ... Edit: With my posted solution it would be something like this: ``` private static void Main(string[] args) { CookieContainer cookieContainer = new CookieContainer(); cookieContainer.Add(new Cookie("name1", "value1", "/", ".testdomain1.com")); cookieContainer.Add(new Cookie("name2", "value1", "/path1/", ".testdomain1.com")); cookieContainer.Add(new Cookie("name2", "value1", "/path1/path2/", ".testdomain1.com")); cookieContainer.Add(new Cookie("name1", "value1", "/", ".testdomain2.com")); cookieContainer.Add(new Cookie("name2", "value1", "/path1/", ".testdomain2.com")); cookieContainer.Add(new Cookie("name2", "value1", "/path1/path2/", ".testdomain2.com")); CookieCollection cookies = GetAllCookies(cookieContainer); Console.WriteLine(JsonConvert.SerializeObject(cookies, Formatting.Indented)); Console.Read(); } ```

Original source

Related problems