Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CookieException with CookieContainer: The 'Path' part of the cookie is invalid

I am trying to set the path of a cookie but I am always recieving the error:

CookieException: The 'Path'='/applogin' part of the cookie is invalid.

the code looks like this:

Cookie newCookie = new Cookie("JSESSIONID", session.SessionId, "/applogin", "domain.com");
newCookie.Secure = true;
webRequest.CookieContainer.Add(new Uri(@"https://domain.com"), newCookie);

the exception is then throwen on the last line... can anyone point me in the right direction?

like image 548
Wooolli Avatar asked Jan 22 '13 10:01

Wooolli


1 Answers

In your case you have two urls: one is a https://domain.com and second one is a https://domain.com/applogin. Let's assume that CookieContainer contains your cookie for path /applogin. This means that if you will try to retrieve list of cookies for url https://domain.com/applogin - you will get one cookie. If you will try to retrieve cookies for url https://domain.com - you will get 0 cookies.

Now let's look on your sample. You have a cookie for https://domain.com/applogin and you are trying to add it to CookieContrainer for url https://domain.com. CookieContainer verifies that this cookie cannot be used for specific url, because it was issues for different url. In your case you need to change line where you add cookie:

webRequest.CookieContainer.Add(new Uri(@"https://domain.com/applogin"), newCookie);

Or I guess you want to use this cookie for whole domain.com - then you need to change how you create it to

Cookie newCookie = new Cookie("JSESSIONID", session.SessionId, "/", "domain.com");
like image 183
outcoldman Avatar answered Sep 26 '22 07:09

outcoldman