【发布时间】:2018-01-01 14:13:42
【问题描述】:
我有一个 Angular(4) 客户端 (localhost:4200),它调用 ASP MVC CORE 2 WebApi。其中一个调用 http://localhost:5000/api/session/resume 会在响应中返回一个 cookie。
在操作方法中,我返回了 3 个 cookie 用于测试目的。
[AllowAnonymous, HttpPost, Route("api/session/resume")]
public async Task<AccountSignInResponse> Resume([FromBody]SessionResumeCommand command)
{
AccountSignInResponse apiResponse = await Mediator.Send(command);
if (!apiResponse.HasErrors) {
Response.Cookies.Append("TestCookie", ..., new CookieOptions
{
Domain = "localhost",
Expires = DateTimeOffset.Now.AddDays(100),
HttpOnly = false
});
Response.Cookies.Append("TestCookie4200", ..., new CookieOptions
{
Domain = "localhost:4200",
Expires = DateTimeOffset.Now.AddDays(100),
HttpOnly = false
});
Response.Cookies.Append("TestCookie5000", ..., new CookieOptions
{
Domain = "localhost:5000",
Expires = DateTimeOffset.Now.AddDays(100),
HttpOnly = false
}); }
return apiResponse;
}
此请求的标头是
Request URL:http://localhost:5000/api/session/resume
Request Method:POST
Status Code:200 OK
Remote Address:[::1]:5000
Referrer Policy:no-referrer-when-downgrade
响应头是
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Vary: Origin
Server: Kestrel
Set-Cookie: TestCookie=XXVtPqCdZ%2BBt9IbhP5Bi7sOLZ%2F%2BELB4fZ0rFArkM%2Be4%3D; expires=Fri, 03 Nov 2017 09:47:28 GMT; domain=localhost; path=/
Set-Cookie: TestCookie4200=XXVtPqCdZ%2BBt9IbhP5Bi7sOLZ%2F%2BELB4fZ0rFArkM%2Be4%3D; expires=Fri, 03 Nov 2017 09:47:28 GMT; domain=localhost:4200; path=/
Set-Cookie: TestCookie5000=XXVtPqCdZ%2BBt9IbhP5Bi7sOLZ%2F%2BELB4fZ0rFArkM%2Be4%3D; expires=Fri, 03 Nov 2017 09:47:28 GMT; domain=localhost:5000; path=/
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://localhost:4200
X-SourceFiles: =?UTF-8?B?QzpcZGV2XFhlcnhlc1xYZXJ4ZXMtU2VydmVyXFNlcnZlclxhcGlcc2Vzc2lvblxyZXN1bWU=?=
X-Powered-By: ASP.NET
Date: Wed, 26 Jul 2017 09:47:28 GMT
如您所见,cookie 是从 http://localhost:5000/api/session/resume 调用返回的,但它们并未存储在 Chrome、Edge 或 Firefox 中的本地 cookie 中。因此,当进一步请求图像和其他资源时,我只看到另一个 cookie (cookieLawSeen),而不是这个熟的。
当我在所有这些浏览器中浏览 localhost 的 cookie 时,我在存储中看不到任何 SessionToken。但是,如果我在 F12 开发人员工具中查看请求,我可以单击 [Cookies] 选项卡并看到 ResponseCookies 包含所有三个 cookie。
【问题讨论】:
-
我有同样的问题。清除缓存并删除浏览器数据是我的解决方案。
-
这不是我的解决方案,我只是第一次在该站点上使用了 firefox。无论如何我都试过了,但没有运气
-
从 Angular 进行 api 调用时是否设置了 withCredentials` property to true?类似
this.http.get('http://...', { withCredentials: true }) -
我没有意识到 withCredentials 会允许接收 cookie,我以为它只是用于发送。那行得通,谢谢!添加它作为答案,我会接受它
标签: asp.net-mvc cookies asp.net-web-api asp.net-core