使用 .net (C#) System.Net.Cookie 处理 cookie 值中的逗号

发布于 2024-07-27 01:48:01 字数 1721 浏览 4 评论 0原文

我正在创建一个客户端来访问网站并登录并自动执行一些任务,但是他们最近更新了他们的 cookie(无论出于何种原因......)在其标识 cookie 中包含一个逗号。

例如,Cookie 将具有类似于以下的值:

a,bcdefghijklmnop

问题是,根据 msdn 您不能在 cookie 值中使用逗号或句点。 我正在寻找一种方法来解决这个限制,一些方法可以使.net的Cookie与逗号很好地配合。 我发现服务器确实向客户端发送了一个“SET-COOKIE”标头,我猜这就是正在解析的内容,但这似乎也显然给命令和分号赋予了特殊含义(因此限制.NET 本身内部的类)。

但是,IE、Firefox 等浏览器如何正确处理 cookie(正如它们显然所做的那样,因为该网站在我测试过的任何浏览器中都运行良好。)是否有一种方法可以强制执行此操作.NET 中的行为?

任何帮助将不胜感激,谢谢。

--- 编辑 ---

一些附加信息:

我的代码看起来像这样:

request = (HttpWebRequest)WebRequest.Create(URI); 
request.CookieContainer = Program.client.cookieJar;

cookieJar 在 Program.client 中定义为:

CookieContainer cookieJar = new CookieContainer();

当我循环并打印 CookieContainer 中的所有 cookie 时,我得到类似这样的内容: (cookies ,格式为:“name”->“value”)

"normal_cookie" -> "i am the value" 
"messedup_cookie" -> "a" 
"bcdefghijklmnop" -> "" 

// What I should get is this: 
"normal_cookie" -> "i am the value" 
"messedup_cookie" -> "a,bcdefghijklmnop" 

问题的核心似乎是逗号和分号是 SET-COOKIE 标头字符串中的保留字符...但是浏览器如何处理这个问题呢? 我可能可以自己解析该字符串,但我不知道如何解决诸如此类的情况:(HTTP 标头,格式为:“名称”->“值”)

"Set-Cookie" -> "messedup_cookie=a,bcdefghijklmnop; path=/; domain=.domain.com; expires=Sat, 15-Aug-2009 09:14:24 GMT,anothervariable=i am the value;"

如您所见,过期部分看起来使用逗号而不是分号来将其与下一个变量区分开来。 据我所知,它的格式是:

cookie1_var1=value; cookie1_var2=value,cookie2_var1=value; cookir2_var2=value

但如果这是真的,是否有一种优雅的方法来处理其中一个值中可能出现的逗号?

I'm creating a client to visit a website and log in + do some tasks automatically, however they recently updated their cookies to (for whatever reason...) contain a comma inside their identification cookie.

So for example, the Cookie will have a value similar to this:

a,bcdefghijklmnop

The problem is, according to msdn you can't use a comma nor a period inside a cookie's value. What I'm looking for is a way around this limitation, some way to make .net's Cookie's work nice with the commas. I've found that the server does send a 'SET-COOKIE' header to the client and I'm guessing that's what is being parsed, but that also seems to obviously give special meaning to commans and semicolons as well (thus the limitation of the class inside .NET itself).

But then how does a browser such as IE, Firefox, etc... handle the cookie properly (as they clearly do, since the website works fine in any browsers I've tested it with.) Is there maybe a way to force this behaviour in .NET?

Any help would be appreciated, thanks.

--- EDIT ---

some additional information:

My code looks something like this:

request = (HttpWebRequest)WebRequest.Create(URI); 
request.CookieContainer = Program.client.cookieJar;

Where cookieJar is defined in Program.client as:

CookieContainer cookieJar = new CookieContainer();

When i loop through and print out all the cookies in the CookieContainer, I get something like this: (cookies, in the format: "name" -> "value")

"normal_cookie" -> "i am the value" 
"messedup_cookie" -> "a" 
"bcdefghijklmnop" -> "" 

// What I should get is this: 
"normal_cookie" -> "i am the value" 
"messedup_cookie" -> "a,bcdefghijklmnop" 

The core of the problem seems to be that commas and semi colons are reserved characters in the SET-COOKIE header string...but then how do browsers handle this? I can probably parse the string myself but I don't know how to get around situations such as this one: (HTTP header, in the format: "name" -> "value")

"Set-Cookie" -> "messedup_cookie=a,bcdefghijklmnop; path=/; domain=.domain.com; expires=Sat, 15-Aug-2009 09:14:24 GMT,anothervariable=i am the value;"

As you can see, the expires section looks for a comma instead of a semicolon to differentiate itself from the next variable. As far as I can tell, it's in the format:

cookie1_var1=value; cookie1_var2=value,cookie2_var1=value; cookir2_var2=value

But if that's true, is there an elegant way to deal with commas that may occur inside one of the values?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寒尘 2024-08-03 01:48:01

根据 以下内容文章中,您应该考虑使用 UrlEncode 和 UrlDecode 在 cookie 中存储值。

private void SetCookie()
{
    HttpCookie cookie = new HttpCookie("cookiename");
    cookie.Expires = DateTime.Now.AddMonths(24);
    cookie.Values.Add("name", Server.UrlEncode(txtName.Text));
    Response.Cookies.Add(cookie);
}

private void GetCookie()
{
    HttpCookie cookie = Request.Cookies["cookiename"];
    if (cookie != null)
    {
        txtName.Text = Server.UrlDecode(cookie.Values["name"]);
    }
}

永远不要在 ASP.NET 中对 cookie 进行 HTML 编码!这会导致黄色
死亡屏幕和异常
说明 cookie 包含
危险人物。

MSDN 也有一篇关于该主题的文章

ASP.NET 不编码或取消编码
UrlEncode 格式的 cookies
默认。 结果,您可能
遇到意想不到的行为
ASP.NET 应用程序。

According to the following article, you should consider UrlEncode and UrlDecode for storing values in cookies.

private void SetCookie()
{
    HttpCookie cookie = new HttpCookie("cookiename");
    cookie.Expires = DateTime.Now.AddMonths(24);
    cookie.Values.Add("name", Server.UrlEncode(txtName.Text));
    Response.Cookies.Add(cookie);
}

private void GetCookie()
{
    HttpCookie cookie = Request.Cookies["cookiename"];
    if (cookie != null)
    {
        txtName.Text = Server.UrlDecode(cookie.Values["name"]);
    }
}

Don’t ever HTML encode a cookie in ASP.NET! It results in a yellow
screen of death and an exception
stating that the cookie contains
dangerous characters.

MSDN also has an article on the subject.

ASP.NET does not encode or unencode
cookies in UrlEncode format by
default. As a result, you may
encounter unexpected behavior in
ASP.NET applications.

独闯女儿国 2024-08-03 01:48:01

为了处理 cookie,由于 cookie 本质上是一个字符串,因此您可能希望在设置 cookie 值之前尝试对 cookie 值进行 URL 编码,然后在提取该值时对其进行解码。

即:

Response.Cookies["Value1"] = Server.UrlEncode(sCookieValue);

类似地:

string sCookieValue = Server.UrlDecode(Request.Cookies["Value1"]);

To handle the cookie, since a cookie is essentially a string, you may like to try URL Encoding the cookie value before setting it, and then Decoding it when you pull out the value.

i.e.:

Response.Cookies["Value1"] = Server.UrlEncode(sCookieValue);

and similarly:

string sCookieValue = Server.UrlDecode(Request.Cookies["Value1"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文