浏览器不会存储 cookie

Browser won't store cookie

我是 运行 angular 4 客户端 localhost:3000,它正在与 WEB API localhost:5000 进行通信。我的问题是,当服务器指示它这样做时,我无法弄清楚为什么浏览器拒绝存储 cookie。

客户请求Headers

Request URL:https://local.dev:5000/user/list?page=1&column=Email&listOrder=2
Request Method:POST
Status Code:200 OK
Remote Address:127.0.0.1:5000
Referrer Policy:no-referrer-when-downgrade
Accept:application/json
Accept-Encoding:gzip, deflate, br
Accept-Language:en-NZ,en-GB;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:no-cache
Connection:keep-alive
Content-Length:0
Content-Type:application/json
DNT:1
Host:local.dev:5000
Origin:https://localhost:3000
Pragma:no-cache
Referer:https://localhost:3000/user

服务器响应Headers:

Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:https://localhost:3000
Cache-Control:no-cache
Content-Type:application/json; charset=utf-8
credentials:include
Date:Tue, 09 May 2017 08:22:25 GMT
Expires:-1
Pragma:no-cache
Server:Kestrel
Set-Cookie: <cookiename>=<somedata>; expires=Tue, 09 May 2017 09:22:23 GMT; domain=local.dev; path=/; secure; httponly
Transfer-Encoding:chunked
Vary:Origin
withCredentials:true

如您所见,CORS 也已启用。 HTTPS 用于传输。也尝试过不设置到期日期。 cookie 的域设置为 local.dev。我还尝试了 /localhost

之后 Chrome 的调试视图不显示 cookie。只是一些默认 angular cookie。因此我假设 set-cookie header 由于某种原因被忽略了。

求助!

对于跨域 AJAX 请求,您需要明确指定 cookie 应随请求一起发送,并在响应中接受。

这是使用 XMLHttpRequest 对象的 withCredentials 标志完成的。

在Angular中,您可以将其设置为$http服务的选项,另请参阅AngularJS withCredentials