Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What can cause a cookie not to be set on the client?

I have a web application that uses jQuery.ajax to perform a request to another host (right now actually the same because I'm using different ports of "localhost"). The server then returns a cookie.

The cookie value in the HTTP response as shown in Chrome's Dev Tools is

Set-Cookie: MyUserSession=JxQoyzYm1VfESmuh-v22wyiyLREyOkuQWauziTrimjKo=;expires=Sun, 10 Feb 2013 22:08:47 GMT;path=/api/rest/

and so has an expiry of 4 hours in the future.

However, the cookie does not get stored and sent with subsequent requests (tested in both Chrome and Firefox). I first thought it must be "10-Feb-2013" instead of "10 Feb 2013" but that doesn't make a difference. Chrome also shows "Expires" as "Invalid date" on the cookies tab of the response, but that might as well be a Dev Tools bug.

Any ideas?

like image 249
AndiDog Avatar asked Feb 10 '13 18:02

AndiDog


People also ask

Why is my cookie not getting set?

This happens with the session cookies are disabled. Restart your server and then try to set the cookie. They should immediately be available. Show activity on this post.

What happen when cookie is not allowed on client browser?

If a user does not accept cookies, he cannot use any of the functionality enabled by them. Which means pretty much the whole internet would break for that user, which is why in this day and age there's virtually nobody who has cookies disabled entirely.

Can cookie be set on client side?

There is no difference. A regular cookie can be set server side or client side. The 'classic' cookie will be sent back with each request. A cookie that is set by the server, will be sent to the client in a response.

Are cookies stored on the client?

Cookies are arbitrary pieces of data, usually chosen and first sent by the web server, and stored on the client computer by the web browser. The browser then sends them back to the server with every request, introducing states (memory of previous events) into otherwise stateless HTTP transactions.


2 Answers

I think I found the solution. Since during development, my server is at "localhost:30002" and my web app at "localhost:8003", they are considered different hosts regarding CORS. Therefore, all my requests to the server are covered by CORS security rules, especially Requests with credentials. "Credentials" include cookies as noted on that link, so the returned cookie was not accepted because I did not pass

xhrFields: {
  withCredentials: true
}

to jQuery's $.ajax function. I also have to pass that option to subsequent CORS requests in order to send the cookie.

I added the header Access-Control-Allow-Credentials: true on the server side and changed the Access-Control-Allow-Origin header from wildcard to http://localhost:8003 (port number is significant!). That solution now works for me and the cookie gets stored.

like image 78
AndiDog Avatar answered Nov 09 '22 00:11

AndiDog


After struggling with a similar scenario (no CORS) for hours, I found out another potential reason: be sure to explicitly set the path for the cookie.

My front-end app was making a call to HOST_URL/api/members/login, and this was returning the right Set-Cookie header, with no path.

I could see the cookie under Response Cookies in Chrome DevTools, but subsequent requests were not including it. Went to chrome://settings/cookies, and the cookie was there, but the path was /api/members.

Specifying root path when setting the cookie at server-side fixed the issue.

like image 27
Manuel Pedrera Avatar answered Nov 09 '22 00:11

Manuel Pedrera