3
votes

I'm making a call to a server via ajax (angularJs, but tried jQuery as well). Response contains a cookie, however that cookie is not persisted on a client for some reason. I can see Set-cookie header in a response, but cookie doesn't show up in development tools of Chrome, nor its available in subsequent requests on a server.

This is a server code (this is WebAPI):

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Headers.AddCookies(new[] { new CookieHeaderValue("auth", "my cookie content") { Expires = DateTime.Now.AddDays(7), Domain = "/" } });
IContentNegotiator negotiator = Configuration.Services.GetContentNegotiator();
ContentNegotiationResult result = negotiator.Negotiate(typeof(LoggedInModel), Request, Configuration.Formatters);
response.Content = new ObjectContent<LoggedInModel>(new LoggedInModel { Email = model.Email }, result.Formatter);
return response;

There's nothing to it, really, and again, I can see the cookies header. This is a cookie header in a response:

Set-Cookie:auth=some-value-here; expires=Mon, 25 Mar 2013 20:39:43 GMT; domain=/

I've tried to run this from IIS, so its not localhost-something - doesn't help.

Its not a CORS, same server, ajax calls are made to "/api/blah" type of addresses.

Any ideas what am I missing ?

1

1 Answers

-1
votes

Needed to set a path on cookie... Duh! Basically when creating a cookie make sure you set a path property:

var cookie = new CookieHeaderValue("auth", "blah") { Expires = DateTime.Now.AddDays(7), Path = "/", HttpOnly = true };

Again, this is web api, MVC sets this by default, I believe - at least I didn't have to specify path when creating a cookie.