更新1:经过更多的研究,我不确定这是否可行,
I created a UserVoice entry on fixing it.
我正在尝试在app退出时保存CookieContainer或者当Tombstoning发生但我遇到了一些问题.
I’ve tried to save CookieContainer in the AppSettings but when loaded, the cookies are gone.
Researching this internally, DataContractSerializer cannot serialize cookies.
This seems to be a behavior that Windows Phone inherited from Silverlight's DataContractSerializer.
经过更多的研究后,似乎解决方法是从容器中取出饼干并以另一种方式保存它们.这很好,直到我遇到另一个障碍.我无法使用.mydomain的Uri获取GetCookies.我相信它是因为this bug.我可以在域名表中看到cookie,.mydomain.com但GetCookies不适用于该特定的cookie.
There is also a problem with getting cookies out of a container too
when the domain begins with a .:
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"));
我找到了一个解决方法,使用反射迭代域表并删除’.’字首.
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];
}
}
}
只有在调用InvokeMember时,才会在SL中抛出MethodAccessException.这并没有真正解决我的问题,因为我需要保留的一个cookie是HttpOnly,这是CookieContainer的原因之一.
If the server sends HTTPOnly cookies, you should create a
System.Net.CookieContainer on the request to hold the cookies,
although you will not see or be able to access the cookies that are
stored in the container.
那么,有什么想法吗?我错过了一些简单的事吗?是否有其他方法可以保存CookieContainer的状态,或者我是否需要保存用户信息(包括密码)并在每次应用程序启动和从逻辑删除返回时重新进行身份验证?
最佳答案 即使使用Reflection,也无法在WP7中访问程序集外部的私有成员.这是一项安全措施,可确保您无法调用内部系统API.
看起来你可能运气不好.