如何在发出 AJAX 跨源请求时防止 "cookie associated with a cross-site resource set without SameSite attribute" 警告?

How do I prevent the "cookie associated with a cross-site resource set without SameSite attribute" warning when making AJAX Cross origin requests?

所以,我有两个站点 http://localhost/ 和 http://3rdPartyLocallyHostedAPI/(不是真实姓名)- 都是本地 Intranet 站点,并且由于 3rdPartyLocallyHostedAPI 因为它是同名的,localhost 必须向它发出 CORS 请求。

这些请求工作正常,数据返回或可以按预期发布到 3rdPartyLocallyHostedAPI,但是显示此警告:

A cookie associated with a cross-site resource at http://3rdPartyLocallyHostedAPI/ was set without the SameSite attribute. A future release of Chrome will only deliver cookies with cross-site requests if they are set with SameSite=None and Secure. You can review cookies in developer tools under Application>Storage>Cookies and see more details at https://www.chromestatus.com/feature/5088147346030592 and https://www.chromestatus.com/feature/5633521622188032.

现在,我查看了多个答案,例如 , and ,其中指出需要在服务器上设置 SameSite 属性,这没有任何意义,因为两个 cookie 存在问题(ss-pid 和 ss-id) 是在请求​​中设置的,不是在响应中返回的吗?这让我感到困惑,因为我不知道如何或在何处进行更改以确保将这些 cookie 上的 SameSite 策略设置为 nonesecure.

我认为执行 AJAX 请求的 jQuery 有问题:

// trimType and queryValue are determined elsewhere by some jQuery selections, their values are not important to the question being asked.
$.ajax({
    url: 'http://3rdPartyLocallyHostedAPI?q=' + trimType + '?q=' + queryValue + '&resultsOnly=true',
    data: {
        properties: (trimType === 'Record') ? 'Title,Number,RecordRecordType' : 'NameString'
    },
    xhrFields: {
        withCredentials: true
    },
    dataType: 'json'
}).done(function (data) {
    if (data.Results.length > 0) {
        $resultsPane.html('');
        for (var i = 0; i < data.Results.length; i++) {
            // Not relevant to the question being asked so removed, only some jQuery in here to display results on page.
        }
    } else {
        $resultsPane.html('<p class="py-1 pl-1 list-group-item text-muted">No Results found.</p>');
    }
}).fail(function () {
    $resultsPane.html('<p class="py-1 pl-1 list-group-item text-muted">No Results found.</p>');
});

当它没有设置 withCredentials = true 属性 时,因此针对 API 进行匿名身份验证(只提供有限的访问权限,因此需要通过 windows 凭据),SameSite 警告不会出现。这是请求 header:

GET /CMServiceAPIAuth/Location?q=%22SDC%20*%22&resultsOnly=true&properties=NameString HTTP/1.1
Host: serverName
Connection: keep-alive
Accept: application/json, text/javascript, */*; q=0.01
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36
Origin: http://localhost:64505
Referer: http://localhost:64505/Home/DisplayRecord
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en-US;q=0.9,en;q=0.8
Cookie: ss-pid=OQtDrnmok62FvLlZPnZV; ss-id=cIaIcS3j0jmoouAaHHGT

chrome 有问题的两个 cookie 是 ss-pidss-id,响应 headers 没有传回 cookie:

HTTP/1.1 200 OK
Cache-Control: private,no-cache
Content-Type: application/json; charset=utf-8
Vary: Accept
Server: Microsoft-IIS/8.5
X-Content-Type-Options: nosniff
X-Powered-By: ServiceStack/4.512 NET45 Win32NT/.NET
X-AspNet-Version: 4.0.30319
Access-Control-Allow-Origin: http://localhost:64505
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST,GET,OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Mon, 27 Jul 2020 07:02:06 GMT
Content-Length: 1597

所以,考虑到所有这些,有人可以解释我哪里出错了吗?我是否需要对 jQuery AJAX 进行更改以防止出现此警告(并因此在警告提醒我发生更改时防止将来出现问题) - 或者,我是否真的需要设置一个服务器上的附加 header,我想知道是否在 pre-flight OPTIONS 请求中它是否试图找出请求的 SameSite 设置或类似的东西?

通过 IIS 模块,我确实可以向服务器发送的响应添加额外的 headers,所以如果需要的话,我可以做到 - 我只是不太了解了解警告是由哪一端引起的,并希望人们能提供任何解释。

好吧,我想我已经做了足够的研究来找出我面临的问题,所以我会回答我自己的问题。

所以,真正帮助我真正理解 SameSite 是关于什么的一个页面是 this one,所以对于任何使用 SameSite 有问题的人,请阅读一下,以便您了解它背后的原因以及它是如何发生的有效。

阅读并看到这个答案后 to another post 帮助我把这些点联系起来。我将正在处理的网站部署到实际的 Web 服务器上,发现以下是响应 header:

HTTP/1.1 200 OK
Cache-Control: private,no-cache
Content-Type: application/json; charset=utf-8
Vary: Accept
Server: Microsoft-IIS/10.0
X-Content-Type-Options: nosniff
X-Powered-By: ServiceStack/4.512 NET45 Win32NT/.NET
X-AspNet-Version: 4.0.30319
Set-Cookie: ss-pid=0QyVIKf4edkAKd2h4be5; expires=Fri, 27-Jul-2040 09:58:39 GMT; path=/; HttpOnly
Set-Cookie: ss-id=fmM1WQsDxXGfR8q9GL6e; path=/; HttpOnly
Access-Control-Allow-Origin: http://server
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: POST,GET,OPTIONS
Access-Control-Allow-Headers: Content-Type, Authorization
Persistent-Auth: true
WWW-Authenticate: Negotiate oYG2MIGzoAMKAQChCwYJKoZIgvcSAQICooGeBIGbYIGYBgkqhkiG9xIBAgICAG+BiDCBhaADAgEFoQMCAQ+ieTB3oAMCARKicARusah2q1K2ACHwoq1n6DCNq5rx/HFdbK5sU9EohnxrRSpzmelskTTa9xmW8wgeUdwRNQCqMsD/dZ/pUjhdl2CVWjmFZZAfnKl6JEker+s79E9uFXThZZKnqfpqEgSvvqSYpp1KMkaYBYd1uf5mRyE=
Date: Mon, 27 Jul 2020 09:58:40 GMT
Content-Length: 1597

服务器发出两个 Set-Cookie header 来存储 ss-idss-pid 的值。这些 cookie apparently stand for permanant session ID and session ID 由 ASP.NET 发布,用于跟踪 session。浏览器不接受和设置这两个 cookie,因为它们缺少 SameSite=none 设置和 Secure 设置 - 这些是我在上面谈论的 post 中提到的两个 cookie。

所以要解决这个问题,首先我需要切换到对 API(可能还有网站本身)使用 https - 我已经完成了,并且想出了如何获得第三个派对 API 在它的 session 相关 cookie 中设置 SameSite 属性。

因此,对于希望完全控制您的 API 等的其他人,您应该能够在响应中设置 creating/sending cookie 时设置这些属性,因此从通过设置 SameSite=NoneSecure.

将站点转移到其他域