8

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?

svick
  • 236,525
  • 50
  • 385
  • 514
Wooolli
  • 81
  • 1
  • 1
  • 3

3 Answers3

12

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");
outcoldman
  • 11,584
  • 2
  • 26
  • 30
0

In some browsers, the path is case sensitive. Make sure all references to "/applogin" match the case exactly.

http://msdn.microsoft.com/en-us/library/ms178194%28v=vs.85%29.aspx

Justin Loveless
  • 524
  • 1
  • 10
  • 25
  • 1
    The URL path is *always* case sensitive. – Gumbo Apr 03 '13 at 20:27
  • 1
    Windows web servers are case insensitive while Linux/Unix are case sensitive. This is because windows' file system is case insensitive while Linux/Unix is case sensitive. However, when performing a cookie request the browser never sends the cookie path to the server, it compares it with what the server returns from the name/value pairs. I believe all browsers are case sensitive about this, but I am not certain. – Justin Loveless Apr 03 '13 at 20:59
  • URL paths are case insensitive, no matter how a web server decides to map URL paths to file system paths. – Gumbo Apr 03 '13 at 21:28
-1

Cookie name: -cant be null or empty -cant be start and end with ' ' -cant be include service symbols, such as \r, '\n', '\t', '=', ';',',' -cant be start with '$'enter image description here

enter image description here