CookieContainer does not store cookies for internationalized domain names

I am trying to authorize in a Cyrillic domain using WebClient. Authorization goes through several stages with redirection between normal and punicode domains. The problem is that HttpWebRequest cannot store cookies in the designated CookieContaier if it was set by the punycode domain. For example, this code will raise a CookieException.

var cookie = new Cookie("test_cookie", "test_value", "/", ".xn----7sbcca6bi0ak9b0a6f.xn--p1ai"); var container = new CookieContainer().Add(cookie); 

The problem is compounded by the fact that the answer, which sets the redirect of cookies to another page, i.e. after WebClient.UploadValues ​​(...) has been executed, there is no cookie information in WebClient.ResponseHeaders.

The following is the normal authorization process (using a browser).

 Method Result Received Type URL RedirectURL Set-Cookie POST 302 1,18 K text/html http://xn----7sbcca6bi0ak9b0a6f.xn--p1ai/admin/login http://xn----7sbcca6bi0ak9b0a6f.xn--p1ai/admin sess_id=.......; expires=Mon, 06-Jun-2016 07:20:57 GMT; Max-Age=31536000; path=/; domain=.xn----7sbcca6bi0ak9b0a6f.xn--p1ai; httponly GET 302 722 text/html http://xn----7sbcca6bi0ak9b0a6f.xn--p1ai/admin /admin/orders GET 200 200,00 K text/html http://xn----7sbcca6bi0ak9b0a6f.xn--p1ai/admin/orders 

Is there a workaround?

+6
source share
1 answer

Are you sure the problem with IDN?

The following code snippet (which matches yours, but with a second line split for compilation)

 var cookie = new Cookie("test_cookie", "test_value", "/", ".xn----7sbcca6bi0ak9b0a6f.xn--p1ai"); var container = new CookieContainer(); container.Add(cookie); container.GetCookies(new Uri("http://test.xn----7sbcca6bi0ak9b0a6f.xn--p1ai")).Dump(); 

does not throw CookieException (executed from LINQPad). Could the problem be in the name and / or value that you are trying to set for the cookie? What exact message do you get from a CookieException?

+1
source

Source: https://habr.com/ru/post/988674/


All Articles