问题:
下面的 ajax 请求代码在 Chrome 中运行时 Chrome 总是不发送 Cookie ,而在 Safari 与 Firefox 都正常,请问如何解决这个问题?
$.ajax({
url: 'xxx',
type: 'put',
xhrFields: { withCredentials: true },
crossDomain: true });
回答1:
在 stackoverflow 上找到了答案:
Theres new draft on cookie policy, called SameSite, currently implemented by Chrome and Opera.
Basically, cookies marked with SameSite=Strict are not sent with CORS request event if you set xhr.withCredentials = true;
In order to make it work, you have to disable SameSite policy on particular cookie. In case of ASP.NET Core 2.0 authetication cookie it was:
services.AddAuthentication(...)
.AddCookie(option => option.Cookie.SameSite = SameSiteMode.None)
.AddOpenIdConnect(...)