我有一個Angular(4)客戶端(localhost:4200),它調用一個ASP MVC CORE 2 WebApi。其中一個調用http://localhost:5000/api/session/resume
會返回一個cookie以及響應。爲什麼我的瀏覽器不存儲ASP MVC CORE 2 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
正如你所看到的,正在從http://localhost:5000/api/session/resume
調用返回的餅乾,但他們沒有存儲在Chrome,Edge或Firefox中的本地Cookie中。因此,當進一步請求圖像和其他資源時,我只看到另一個cookie(cookieLawSeen),而不是這個熟。
當我在所有這些瀏覽器中瀏覽localhost的cookie時,在存儲中看不到任何SessionToken。但是,如果我查看F12開發人員工具中的請求,則可以單擊[Cookies]選項卡並查看ResponseCookies包含所有三個Cookie。
我有相同的象徵。清理緩存並刪除瀏覽器數據是我的解決方案。 –
這不是我的解決方案,我只是第一次在該網站上使用firefox。無論如何,我嘗試過,但沒有運氣 –
你設置[withCredentials'屬性爲true](https://stackoverflow.com/questions/35602866/how-to-send-cookie-in-request-header-for-all-the -requests-in-angular2)當你從角度做api調用時?就像'this.http.get('http:// ...',{withCredentials:true})' – Set