C# WebApi + Angular 4 Set-Cookie doesn't save

2019-09-07 03:12发布

问题:

I'm doing an http request to my API that is supposed to send me back two cookies with the Set-Cookie Header.

It seems that the cookie is correctly send back for this Init Request but they are not saved in Chrome, so for later requests the cookies are not sent.

My Init response headers are :

Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:content-type
Access-Control-Allow-Origin:http://localhost:4200
Cache-Control:no-cache
Content-Length:0
Date:Thu, 20 Apr 2017 09:25:45 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/8.0
Set-Cookie:ASP.NET_SessionId=vpt0f3m4jp2gwghm1mmrxyhm; path=/; HttpOnly
Set-Cookie:ARRAffinity=ccebcfbd458447f3ab209749a770ad1f93d4b4c907a7cac9ef717d5fbf543fe3;Path=/;Domain=xxx
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET

But nothing is stored in the cookies storage of Chrome.

Could it came from the

Expires:-1

Thanks in advance !

回答1:

I guess your C# backend runs at a different port than your Angular application. Cookies are not shared among different ports of the same host. Chrome displays cookies valid for the current tab URL, which is the Angular application. So you cannot see your backend cookies there.

You can check your headers of XHR requests from the Angular application to the C# backend, they should contain the specified cookies (ASP.NET_SessionId, ARRAffinity).