处理内部使用.NET的(C#)System.Net.Cookie cookie的值逗号 [英] handling a comma inside a cookie value using .net's (C#) System.Net.Cookie

查看:2144
本文介绍了处理内部使用.NET的(C#)System.Net.Cookie cookie的值逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要创建一个客户端访问一个网站并登录+自动完成某些任务,但是他们最近更新了自己的饼干(不管出于什么原因...)包含其标识的cookie里面的一个逗号。

因此​​,例如,cookie将有一个类似的值:

  A,bcdefghijklmnop
 

现在的问题是,根据 MSDN 你不能使用逗号,也没有一个时期内的cookie的值。我正在寻找的是解决此限制的方法,一些方法,使.NET的Cookie的工作,不错的逗号。我发现,则服务器将发送一个设置Cookie头到客户端,我猜这就是被解析,但似乎也显然是commans和分号赋予特殊的意义,以及(因而限制里面的类.NET本身)。

但后来如何做一个浏览器,如IE,火狐等..妥善处理该cookie(因为他们清楚这样做,因为该网站工作正常,在我已经与测试它。任何浏览器)是否有可能的方式强制在.NET这种行为?

任何帮助将是AP preciated,谢谢。

---编辑---

一些额外的信息:

我的code看起来是这样的:

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

在哪里cookieJar在Program.client定义为:

 的CookieContainer cookieJar =新的CookieContainer();
 

当我遍历和的CookieContainer打印出所有的饼干,我得到的是这样的:(饼干,格式为:姓名 - >值)

 normal_cookie - > 我是值
messedup_cookie - > 一个
bcdefghijklmnop - > 

//我应该是这样的:
normal_cookie - > 我是值
messedup_cookie - > 一,bcdefghijklmnop
 

问题的核心似乎是逗号和半冒号的set-cookie头字符串中的保留字符...但再怎么做浏览器的处理呢?我大概可以分析字符串自己,但我不知道如何解决像这样的情况:(HTTP头,其格式为:姓名 - >值)

 设置Cookie - > messedup_cookie = A,bcdefghijklmnop;路径= /;域= .domain.com;到期=周六,15月,2009年9点14分24秒GMT,anothervariable =我要的价值;
 

正如你所看到的,过期部分寻找一个逗号,而不是一个分号从下一个变量中脱颖而出。据我所知,它的格式为:

  cookie1_var1 =价值; cookie1_var2 =值,cookie2_var1 =价值; cookir2_var2 =价值
 

但是,如果这是真的,有一种优雅的方式来处理内部的一个值时可能出现的逗号?

解决方案

根据的下面的文章,你应该考虑UrlEn code和UrlDe code在存储的cookie值。

 私人无效的setcookie()
{
    的HttpCookie饼干=新的HttpCookie(cookiename);
    cookie.Expires = DateTime.Now.AddMonths(24);
    cookie.Values​​.Add(名,Server.UrlEn code(txtName.Text));
    Response.Cookies.Add(饼干);
}

私人无效的getCookie()
{
    的HttpCookie饼干= Request.Cookies时[cookiename];
    如果(饼干!= NULL)
    {
        txtName.Text = Server.UrlDe code(cookie.Values​​ [名称]);
    }
}
 

  

不要在ASP.NET永远HTML连接codeA饼干!这导致黄色   死亡和异常的画面   说明该cookie包含   危险的人物。

MSDN也有关于这个问题的文章

  

ASP.NET不连接code或unen code   在UrlEn code画幅的Cookie通过   默认。这样一来,你可以   遇到意外行为   ASP.NET应用程序。

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?

解决方案

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.

这篇关于处理内部使用.NET的(C#)System.Net.Cookie cookie的值逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆