如何修复Chrome中的“由于用户偏好,此Set-Cookie被阻止”?(Stackoverflow SSO Login / Ajax CORS request)

2022-08-30 19:15:03

似乎最近将Chrome更新到版本83.0.4103.116对Cookie处理进行了更改。

我正在为我的用户提供单点登录,将他们登录到多个网站。与Stackoverflow类似,我正在使用Jquery执行AJAX请求

crossDomain: true, 
xhrFields: { withCredentials: true },

在 PHP 中,我允许以下域:

// needed for cross-domain request
header('Access-Control-Allow-Origin: https://www.example.com');
header('Access-Control-Allow-Credentials: true');

但是,现在它不再起作用了。

在开发控制台中,我发现了一个带有工具提示的新警告:

“由于用户偏好,此设置 Cookie 已被阻止”

chrome warning tooltip

如何解决这个问题?



更新:

我只是看到Stackoverflow的单点登录也不再工作了!

enter image description here



PS:一个相关的问题建议告诉您的用户更改Chrome设置,从我的POV来看,我想避免这种情况。想象一下,SO通知数百万用户使Cookie能够进行单点登录...


答案 1

如果您只能在隐身模式下复制这一点,而Pierre Pretorius的答案没有帮助,那么您可能会受到Chrome 83中更改的打击,其中第三方cookie在隐身模式下默认被阻止。查看 https://angel.co/today/stories/chrome-83-arrives-with-redesigned-security-settings-third-party-cookies-blocked-in-incognito-21796

我不认为你可以做很多事情来改变这一点,谷歌打算在未来将其作为默认行为:https://www.theverge.com/2020/1/14/21064698/google-third-party-cookies-chrome-two-years-privacy-safari-firefox

编辑:谷歌至少要到2023年才能实现这一点 https://blog.google/products/chrome/updated-timeline-privacy-sandbox-milestones/


答案 2

传递HTTP标头的站点也需要传递as和,否则cookie不会被保存并被忽略。set-cookieSameSiteNoneSecure

Set-Cookie: qa_session=...; SameSite=None; Secure

在此之前,请阅读安全隐患:https://blog.heroku.com/chrome-changes-samesite-cookie

PHP 代码示例(源代码):

function setcookieSameSite($name, $value, $expire, $path, $domain, $secure, $httponly, $samesite="None")
{
  if (PHP_VERSION_ID < 70300) {
        setcookie($name, $value, $expire, "$path; samesite=$samesite", $domain, $secure, $httponly);
  }
  else {
      setcookie($name, $value, [
          'expires' => $expire,
          'path' => $path,
          'domain' => $domain,
          'samesite' => $samesite,
          'secure' => $secure,
          'httponly' => $httponly,
      ]);
   }
}

推荐