我正在尝试在我的应用程序中设置一个cookie.
这是设置cookie的代码:
public HttpResponseMessage LogIn(UserLoginVM user) { // Do login stuff var cookie = new CookieHeaderValue("STUPID-COOKIE","12345"); cookie.Domain = Request.RequestUri.Host; cookie.Path = "/"; cookie.HttpOnly = true; // Get user's profile HttpResponseMessage res = Request.CreateResponse<UserProfileVM>(HttpStatusCode.OK,profile); res.Headers.AddCookies(new CookieHeaderValue[] { cookie }); return res; }
服务器的响应如下:
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.0 Set-Cookie: STUPID-COOKIE=12345; domain=localhost; path=/; httponly Access-Control-Allow-Origin: * X-AspNet-Version: 4.0.30319 X-SourceFiles: =?UTF-8?B?QzpcUFJPSkVDVFNcU2Ftc2tpcC5TZXJ2aWNlV2ViTmV3XFNhbXNraXAuQXV0aEFQSVxTYW1za2lwLkF1dGhBUElcbG9naW4=?= X-Powered-By: ASP.NET Date: Wed,18 Feb 2015 11:58:07 GMT Content-Length: 8019
请注意以下标题:
Set-Cookie: STUPID-COOKIE=12345; domain=localhost; path=/; httponly
但是,当我在Chrome中的“资源”标签中的“Cookie”下,没有设置任何内容.此外,当我向服务器发送请求时,标题中没有cookie.
这是读取cookie的代码:
CookieHeaderValue cookie = Request.Headers.GetCookies("STUPID-COOKIE").FirstOrDefault();
cookie变量始终为null.
我的应用程序在http://localhost:53998上运行,身份验证服务在http://localhost:60858上运行
我的Chrome版本是40.0.2214.111.
这是一个GIF演示了这个问题:
http://i.imgur.com/q7lkXBz.gif
编辑:这似乎与Chrome不具体.这对FireFox(v35)也不起作用. GIF:http://i.imgur.com/ZewnEtc.gif