使用来自子域的请求发送 cookie

Sending cookie with request from subdomain

我们有以下配置: testing.parentdomain.com

当您访问此域并创建购物篮时,我们会为购物篮值创建一个存储的 cookie。 cookie域设置为.testing.parentdomain.com,是Httponly,路径为/

我们有一个要访问 cookie 的上述子域。 subdomain.testing.parentdomain.com
此子域调用 parent 域上的端点,例如:testing.parentdomain.com/basketData。此调用是 returns JSON.

的 GET 请求

问题
问题是子域在发出请求时似乎没有发送 cookie 值,因此我们没有得到预期的响应。

尝试次数
查看其他问题,我们尝试了 CORS 和凭证更改。
作为附加说明,我们将以下 JS 与 webpack/babel.
捆绑在一起 我们的请求来自 AJAX 如下:

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

服务器为子域和 allow-crendtials 设置了 CORS。在响应中我们可以看到这些已返回。
access-control-allow-credentials: 真
access-control-allow-origin: 上面的子域

是否有任何原因导致 cookie 未随请求一起发送到 parent 域?我们已注销服务器端响应中的 cookie,但它们并不像我们预期的那样存在。

请求Headers

:权限:testing.parentdomain.com
:方法:获取
:路径:/basket/data/
:方案: https
接受:/
accept-encoding: gzip, deflate, br
accept-language: en-GB,en;q=0.9,en-US;q=0.8
来源:https://subdomain.testing.parentdomain.com
推荐人:https://subdomain.testing.parentdomain.com/
sec-fetch-dest: 空
sec-fetch-mode: 科尔斯
sec-fetch-site: same-site
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like壁虎)Chrome/86.0.4240.111 Safari/537.36

回应Headers

access-control-allow-credentials: 真
Access-Control-Allow-Methods:获取、放置、POST、删除、头部、选项
access-control-allow-origin: https://subdomain.testing.parentdomain.com
cache-control: no-cache, no-store
content-length: 2238
content-type: application/json;字符集=utf-8
日期:2020 年 11 月 3 日,星期二 20:39:36 GMT
过期:-1
编译指示:no-cache
服务器:Microsoft-IIS/10.0
set-cookie: AWSALB=N0bcThdgRFzrSfQVNIsffgsvY6T/y2Bp47RZJCueeSLOS7eEjo0AThiElXmww6fy2eynRyyt8gAB8di/Mqy1x+Ds8Ig1TumKkWnQiFvIkoELI/rEYYgyUxbEtUI4;到期 = 2020 年 11 月 10 日,星期二 20:39:36 GMT;路径=/
set-cookie:AWSALBCORS=N0bcThdgRFzrSfQVNIsffgsvY6T/y2Bp47RZJCueeSLOS7eEjo0AThiElXmww6fy2eynRyyt8gAB8di/Mqy1x+Ds8Ig1TumKkWnQiFvIkoELI/rEYYgyUxbEtUI4;到期 = 2020 年 11 月 10 日,星期二 20:39:36 GMT;路径=/; SameSite=None;安全
状态:200
strict-transport-security: max-age=31536000;
变化:起源
x-content-type-选项:nosniff
x-frame-options:同源
x-robots-tag: 无索引
x-ua-compatible: IE=边缘
x-xss-protection: 1;模式=块

即使您是从子域调用主域,这也被视为 cross-origin 请求。

引自 RFC 6454 的“来源”术语:

Q: Why use the fully qualified host name instead of just the "top-
level" domain?

A: Although the DNS has hierarchical delegation, the trust
relationships between host names vary by deployment. For example, at many educational institutions, students can host content at
https://example.edu/~student/, but that does not mean a document
authored by a student should be part of the same origin (i.e.,
inhabit the same protection domain) as a web application for managing grades hosted at https://grades.example.edu/.

所以您所做的所有事情确实需要才能让它发挥作用:

  • access-control-allow-credentials: true
  • access-control-allow-origin: subdomain.testing.parentdomain.com(不是通配符)
  • withCredentials: true 在请求中

SameSite=None cookie attribute is not required in this case because a request from a subdomain to another subdomain of the same domain is considered "same site" (Source).

所以只需检查所有设置是否正确,它应该可以正常工作。

在你的问题开头你说:

The cookie domain is set to .testing.parentdomain.com

但在记录的服务器响应中:

set-cookie: AWSALBCORS=N0bcThdgRFzrSfQVNIsffgsvY6T/y2Bp47RZJCueeSLOS7eEjo0AThiElXmww6fy2eynRyyt8gAB8di/Mqy1x+Ds8Ig1TumKkWnQiFvIkoELI/rEYYgyUxbEtUI4; Expires=Tue, 10 Nov 2020 20:39:36 GMT; Path=/; SameSite=None; Secure

显然缺少 Domain=.testing.parentdomain.com; 参数。

我不知道您使用哪种编程语言来设置 cookie,但我强烈建议您检查您用于在服务器响应中设置 cookie 的调用。