=波波日志 > Asp.Net/C#/WCF > CookieContainer不同子域名下的bug=
CookieContainer不同子域名下的bug
这几天再用httpwebrequest模拟登录某个网站,但是老是登录不了,视乎无法保存cookie一样。但是调试的时候发现CookieContainer确实包含了和firebug监视到的所有cookie。
后来google查找到了一篇文章,原来是.net framework的bug,顶级域名的cookie没有发送出去,导致另外一个二级域名没有获取到顶级域名cookie的值,从而无法生成2级域名的session对应的cookie。
用HttpWebRequest发送请求,附上了CookieContainer(CookieContainer里面确定已经包含了所有需要的 Cookie),但是当发送请求后某些Cookie并没有发送出去,调试了两天,一直觉得是请求的网站设了什么古怪的限制,使请求老是发送不成功,最后用 SocketSniff抓包发现少发送了几个Cookie(因为这些cookie涉及到几个子域名),检查CookieContainer,里面确实有这 几个Cookie,最后只好反编译HttpWebRequest,在类CookieModule里发现方法OnSendingHeaders的如下代码, 这个是把cookie设置到headers里面的关键地方:
问题出在GetCookieHeader这个方法里面,某些Cookie的domain判断错误导致没有附加上去。.Net framework4.0以下的都存在此问题。最后把程序框架调到4.0的,就没有这个问题了。当然4.0以下的框架版本可以用下面的方法处理一下 CookieContainer里面domain。
1. Don't use .Add(Cookie), Use only .Add(Uri, Cookie) method.
2. Call BugFix_CookieDomain each time you add a cookie to the container or before you use .GetCookie or before system use the container.
来源:http://www.cnblogs.com/goody9807/archive/2011/07/19/2110535.html
后来google查找到了一篇文章,原来是.net framework的bug,顶级域名的cookie没有发送出去,导致另外一个二级域名没有获取到顶级域名cookie的值,从而无法生成2级域名的session对应的cookie。
用HttpWebRequest发送请求,附上了CookieContainer(CookieContainer里面确定已经包含了所有需要的 Cookie),但是当发送请求后某些Cookie并没有发送出去,调试了两天,一直觉得是请求的网站设了什么古怪的限制,使请求老是发送不成功,最后用 SocketSniff抓包发现少发送了几个Cookie(因为这些cookie涉及到几个子域名),检查CookieContainer,里面确实有这 几个Cookie,最后只好反编译HttpWebRequest,在类CookieModule里发现方法OnSendingHeaders的如下代码, 这个是把cookie设置到headers里面的关键地方:
+展开
-C#
internal static void OnSendingHeaders(HttpWebRequest httpWebRequest)
{
try
{
if (httpWebRequest.CookieContainer != null)
{
string str;
httpWebRequest.Headers.RemoveInternal("Cookie");
string cookieHeader = httpWebRequest.CookieContainer.GetCookieHeader(httpWebRequest.Address, out str);
if (cookieHeader.Length > 0)
{
httpWebRequest.Headers["Cookie"] = cookieHeader;
}
}
}
catch
{
}
}
{
try
{
if (httpWebRequest.CookieContainer != null)
{
string str;
httpWebRequest.Headers.RemoveInternal("Cookie");
string cookieHeader = httpWebRequest.CookieContainer.GetCookieHeader(httpWebRequest.Address, out str);
if (cookieHeader.Length > 0)
{
httpWebRequest.Headers["Cookie"] = cookieHeader;
}
}
}
catch
{
}
}
问题出在GetCookieHeader这个方法里面,某些Cookie的domain判断错误导致没有附加上去。.Net framework4.0以下的都存在此问题。最后把程序框架调到4.0的,就没有这个问题了。当然4.0以下的框架版本可以用下面的方法处理一下 CookieContainer里面domain。
1. Don't use .Add(Cookie), Use only .Add(Uri, Cookie) method.
2. Call BugFix_CookieDomain each time you add a cookie to the container or before you use .GetCookie or before system use the container.
+展开
-C#
private void BugFix_CookieDomain(CookieContainer cookieContainer)
{
System.Type _ContainerType = typeof(CookieContainer);
Hashtable table = (Hashtable)_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];
}
}
}
{
System.Type _ContainerType = typeof(CookieContainer);
Hashtable table = (Hashtable)_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];
}
}
}
来源:http://www.cnblogs.com/goody9807/archive/2011/07/19/2110535.html
类别:Asp.Net/C#/WCF 作者:波波 日期:2011-10-28 【评论:0】
相关文章
暂时没有评论!
发表留言
热门博文
- IE里Cookie跨域不能读取
- web服务因URL意外地以/**结束,请求格式无法识别
- 去掉隐藏asp.net编译出错aspxerrorpath错误参数
- 解决asp.net验证视图状态 MAC 失败
- 找不到System.Web.Script.Services.ScriptService
- 在aspx,ashx页面挂起线程执行
最新博文
- WCF授权-通过扩展自行实现服务授权
- WCF授权-ASP.NET Roles授权[下篇]
- WCF授权-ASP.NET Roles授权[上篇]
- WCF授权-模拟在WCF中的应用
- WCF授权-模拟(Impersonation)与委托(Delegation)
- WCF授权-基于Windows用户组授权[下篇]
随机博文
- WCF授权-Identity与Principal[上篇]
- 14.5 Web服务(Web Service)
- C# 3.0 In, Like操作
- C#泛型小结
- 18.5 使用WCF服务
- ASP.NET AJAX:在多个UpdatePanle中使用Timer控件 (2)
广告商赞助

