CookieContainer 错误?
我很困惑 CookieContainer 如何处理域,所以我创建了这个测试。 此测试显示 cookieContainer 不会为“example.com”返回任何 cookie,但根据 RFC,它应该返回至少 2 个 cookie。
这不是一个错误吗?
如何让它发挥作用?
以下是有关此错误的讨论:
http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/c4edc965-2dc2-4724-8f08-68815cf1dce6
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
CookieContainer getContainer()
{
CookieContainer result = new CookieContainer();
Uri uri = new Uri("http://sub.example.com");
string cookieH = @"Test1=val; domain=sub.example.com; path=/";
result.SetCookies(uri, cookieH);
cookieH = @"Test2=val; domain=.example.com; path=/";
result.SetCookies(uri, cookieH);
cookieH = @"Test3=val; domain=example.com; path=/";
result.SetCookies(uri, cookieH);
return result;
}
void Test()
{
CookieContainer cookie = getContainer();
lblResult.Text += "<br>Total cookies count: " + cookie.Count + " expected: 3";
Uri uri = new Uri("http://sub.example.com");
CookieCollection coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
uri = new Uri("http://other.example.com");
coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
uri = new Uri("http://example.com");
coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
}
protected void Page_Load(object sender, EventArgs e)
{
Test();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CookieContainer Test Page</title>
</head>
<body>
<form id="frmTest" runat="server">
<asp:Label ID="lblResult" EnableViewState="false" runat="server"></asp:Label>
</form>
</body>
</html>
I'm confused how CookieContainer handles domain, so I create this test.
This test shows cookieContainer doesn't return any cookie for "example.com" but according to RFC it should return at least 2 cookies.
Isn't it a bug?
How make it to work?
Here is a discussion about this bug:
http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/c4edc965-2dc2-4724-8f08-68815cf1dce6
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Net" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
CookieContainer getContainer()
{
CookieContainer result = new CookieContainer();
Uri uri = new Uri("http://sub.example.com");
string cookieH = @"Test1=val; domain=sub.example.com; path=/";
result.SetCookies(uri, cookieH);
cookieH = @"Test2=val; domain=.example.com; path=/";
result.SetCookies(uri, cookieH);
cookieH = @"Test3=val; domain=example.com; path=/";
result.SetCookies(uri, cookieH);
return result;
}
void Test()
{
CookieContainer cookie = getContainer();
lblResult.Text += "<br>Total cookies count: " + cookie.Count + " expected: 3";
Uri uri = new Uri("http://sub.example.com");
CookieCollection coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
uri = new Uri("http://other.example.com");
coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
uri = new Uri("http://example.com");
coll = cookie.GetCookies(uri);
lblResult.Text += "<br>For " + uri + " Cookie count: " + coll.Count + " expected: 2";
}
protected void Page_Load(object sender, EventArgs e)
{
Test();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>CookieContainer Test Page</title>
</head>
<body>
<form id="frmTest" runat="server">
<asp:Label ID="lblResult" EnableViewState="false" runat="server"></asp:Label>
</form>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我刚刚找到了这个错误的修复并在这里讨论:
http://dot-net-expertise .blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
这是解决方案:
每次向容器添加 cookie 时调用 BugFix_CookieDomain 或
在您使用 .GetCookie 之前或在系统使用容器之前。
I just found the fix for this bug and discussed here:
http://dot-net-expertise.blogspot.com/2009/10/cookiecontainer-domain-handling-bug-fix.html
Here is the solution:
Call BugFix_CookieDomain each time you add a cookie to the container or
before you use .GetCookie or before system use the container.
我已针对此问题创建了适用于 Windows 10 / UWP / .NET Core 应用程序的修复程序。 问题在于 CookieContainer 的内部结构不同,但与 .NET Framework 中的内部结构一样糟糕。 所以所接受的解决方案不再有效。
但我没有“修复” CookieContainer ,而是编写了一个 GetCookies() 版本,它使用字符串获取特定域的所有 cookie,而不管它们的“安全性”如何。 " 状态或者它们是否以点为前缀。 请随意修改它,以满足您的需求,我将考虑在未来的 .NET Core 版本中实现它的版本。
I've created a fix for this problem that works on Windows 10 / UWP / .NET Core apps. The issue is that the internals for
CookieContainer
are different, but just as crappy, as they are in the .NET Framework proper. So the accepted solution does not work anymore.But instead of "fixing" the
CookieContainer
, I just wrote a version ofGetCookies()
that gets all the cookies for a particular domain with a string, regardless of their "secure" state or if they are prefixed with a dot. Feel free to modify it as you see fit for your needs, and I'll see about getting a version of it implemented in a future .NET Core release.我因这个问题而失去了一天。 CallMeLaNN 的回复对我没有帮助(我正在使用 .Net 4.5)。 就我而言,问题在于设置请求正文和设置 cookie 的顺序。
在这种情况下,cookie 将不会发送到服务器:
要使其正常工作,需要更改顺序:
Lost my day with this issue. CallMeLaNN's response didn't help me (I'm using .Net 4.5). In my case the problem was in order of setting body of request and settings cookies.
In this case cookies will not be sent to server:
To make it work change the order is required:
这里有一个解决这个错误的技巧:
http://social.microsoft. com/Forums/en-US/netfxnetcom/thread/1297afc1-12d4-4d75-8d3f-7563222d234c
它使用反射。
Here is a hack to get around this bug:
http://social.microsoft.com/Forums/en-US/netfxnetcom/thread/1297afc1-12d4-4d75-8d3f-7563222d234c
It uses reflection.
最后他们会修复它:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback。 aspx?FeedbackID=478521
At last they gonna fix it:
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=478521