update1: After more research, I'm not sure if this is possible, I created a UserVoice entry to fix it .
I try to save a CookieContainer when I exit the application or when Tombstoning happens, but I ran into some problems.
I tried to save the CookieContainer in AppSettings, but the cookies disappeared on loading .
Researching this internally, DataContractSerializer cannot serialize cookies. This seems to be a behavior that Windows Phone inherited from Silverlight DataContractSerializer.
After doing more research, it seemed like the work around was to grab the cookies from the container and store them in a different way. This worked fine until I hit another snag. I can not getcookies with Uri.mydomain.com. I believe in this because of this error . I can see the cookie, .mydomain.com in domaintable, but GetCookies does not work on this particular cookie.
The error is posted here again .
There is also a problem with getting cookies from the container when the domain starts with:
CookieContainer container = new CookieContainer(); container.Add(new Cookie("x", "1", "/", ".blah.com")); CookieCollection cv = container.GetCookies(new Uri("http://blah.com")); cv = container.GetCookies(new Uri("http://w.blah.com"));
I found a job for this, using reflection to iterate over the dominant and remove the ".". Console.
private void BugFix_CookieDomain(CookieContainer cookieContainer) { System.Type _ContainerType = typeof(CookieContainer); var = _ContainerType.InvokeMember("m_domainTable", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetField | System.Reflection.BindingFlags.Instance, null, cookieContainer, new object[] { }); ArrayList keys = new ArrayList(table.Keys); foreach (string keyObj in keys) { string key = (keyObj as string); if (key[0] == '.') { string newKey = key.Remove(0, 1); table[newKey] = table[keyObj]; } } }
Only when an InvokeMember is thrown is a MethodAccessException is thrown in the SL. This really does not solve my problem since one of the cookies I need to save is HttpOnly, which is one of the reasons for CookieContainer.
If the server sends HTTPOnly cookies, you must create a System.Net.CookieContainer at the request of the cookie, although you will not see or be able to access the cookies stored in the container.
So, any ideas? Did I miss something? Is there any other way to keep the CookieContainer state or do I need to save information about users, including the password, and re-authenticate them each time the application starts and when it returns from the tombstone?