【问题标题】:Firefox does not set Cookie received with XHR CORS requestFirefox 未设置通过 XHR CORS 请求接收的 Cookie
【发布时间】:2019-03-04 11:53:41
【问题描述】:

我正在尝试在域“example.com”上构建一个登录站点,该站点将 Ajax 请求发送到域“other_domain.com”,如果凭据正常,则此请求会发回会话 cookie。之后我想重定向到“other_domain.com”网站并想登录。

我有一个适用于 IE11、Edge、Chrome 但不适用于 Firefox 的解决方案,因为当我重定向到“other_domain.com”站点时,Firefox 不会设置返回的 cookie。

这是ajax请求代码:

var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('POST', 'https://other_domain.com/login', true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.onreadystatechange = function () {
    if (this.status == 200 && this.readyState == 4) {

        window.location.replace("https://other_domain.com/app");

    }
};
xhr.send(JSON.stringify(payload));

如果凭据正确,我可以在每个浏览器上看到 OPTIONS 请求成功,并且 AJAX 发布在每个浏览器上都返回 200 OK。

返回的 cookie 具有以下值:

CreationTime: "Fri, 28 Sep 20018 12:48:49 GMT"
Domain: "other_domain.com"
Expires: "Session"
HostOnly: true
HttpOnly: true
LastAccessed: "Fri, 28 Sep 20018 12:48:49 GMT"
Path: "/"
Secure: true
sameSite: "Lax"

标记为重复后的其他信息:

我可以在开发者控制台中看到,上面代码中的 OPTIONS 和 POST 请求返回了以下标头:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: https://example.com

我也可以在 Firefox 的响应中看到具有正确值的 Set-Cookie 标头,但在重定向到 other_domain.com 后未设置 cookie。 此外,我的 Firefox 设置为“接受第三方 cookie 和站点数据”-“始终”

【问题讨论】:

标签: javascript ajax firefox cors


【解决方案1】:

我有同样的问题。查看 Set_Cookie,它是否具有 SameSite=lax 属性。如果是这样,在服务器上设置 cookie 时将该属性设置为 None。 在 .NET 中创建 cookie 时,您可以选择 lax、strict 或 none。

https://docs.microsoft.com/en-us/dotnet/api/system.web.httpcookie.samesite?view=netframework-4.7.2

【讨论】:

  • 感谢您的回答。与此同时,我重构了我的解决方案,它不需要 CORS cookie。我的 cookie 将 samesSite 设置为 lax,因此您的解决方案可能有效 - 但我无法测试。我不知道我是否应该接受你的回答,因为我现在无法检查。
猜你喜欢
  • 2012-04-26
  • 1970-01-01
  • 2016-07-21
  • 2012-04-25
  • 2012-12-22
  • 2020-06-18
  • 2021-05-29
  • 1970-01-01
  • 2013-08-26
相关资源
最近更新 更多