jquery ajax withCredentials:true 在 Chrome 中不起作用

2019-02-26 21:07发布

下面的 ajax 请求代码在 Chrome 中运行时 Chrome 总是不发送 Cookie ,而在 Safari 与 Firefox 都正常,请问如何解决这个问题?

$.ajax({
    url: 'xxx',
    type: 'put',
    xhrFields: { withCredentials: true },
    crossDomain: true });

标签: js chrome
1条回答
Evening l夕情丶
2楼-- · 2019-02-26 21:43

在 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(...)
查看更多
登录 后发表回答