如何跨起源共享cookie ?更具体地说,如何使用Set-Cookie头结合头Access-Control-Allow-Origin?

以下是对我的情况的解释:

我试图在localhost:3000上托管的web应用程序中为运行在localhost:4000上的API设置一个cookie。

似乎我在浏览器中收到了正确的响应头,但不幸的是,它们没有效果。这些是响应头:

HTTP/1.1 200 OK
Access-Control-Allow-Origin: http://localhost:3000
Vary: Origin, Accept-Encoding
Set-Cookie: token=0d522ba17e130d6d19eb9c25b7ac58387b798639f81ffe75bd449afbc3cc715d6b038e426adeac3316f0511dc7fae3f7; Max-Age=86400; Domain=localhost:4000; Path=/; Expires=Tue, 19 Sep 2017 21:11:36 GMT; HttpOnly
Content-Type: application/json; charset=utf-8
Content-Length: 180
ETag: W/"b4-VNrmF4xNeHGeLrGehNZTQNwAaUQ"
Date: Mon, 18 Sep 2017 21:11:36 GMT
Connection: keep-alive

此外,当我使用Chrome开发工具的网络选项卡检查流量时,我可以在响应cookie下看到cookie。然而,我不能看到一个cookie被设置在应用程序选项卡下的存储/ cookie。我没有看到任何CORS错误,所以我假设我还遗漏了其他东西。

有什么建议吗?

我更新:

我正在使用React-Redux应用程序中的请求模块向服务器上的/signin端点发出请求。对于服务器,我使用express。

Express服务器:

res.cookie('token', 'xxx-xxx-xxx', { maxAge: 86400000, httpOnly: true, domain: 'localhost:3000' })

在浏览器中请求:

request.post({ uri: '/signin', json: { userName: 'userOne', password: '123456'}}, (err, response, body) => {
    // doing stuff
})

更新二:

我现在像疯狂一样设置请求和响应头,确保它们在请求和响应中都存在。下面是截图。注意标题Access-Control-Allow-Credentials, Access-Control-Allow-Headers, Access-Control-Allow-Methods和Access-Control-Allow-Origin。看看我在Axios的github上发现的问题,我的印象是所有必需的标题现在都设置好了。然而,还是没有运气……


当前回答

在最新的chrome标准中,如果CORS请求带cookie,它必须打开samesite = none和secure,后端域名必须打开HTTPS,

其他回答

希望这能有所帮助 对于我关于sameSite属性,启用CORS后,我还添加了“CookieSameSite = SameSiteMode。没有一个“ 到启动文件中的CookieAuthenticationOptions

app.UseCookieAuthentication(新CookieAuthenticationOptions { … CookieSameSite = SameSiteMode。没有, … }

在一天多的时间里,我尝试了你所有的建议和更多的建议,我投降了。 Chrome只是不接受我的跨域cookie在本地主机。 没有错误,只是默默地忽略。 我想有http仅饼干更安全的存储令牌。 所以对于localhost来说,代理听起来是最好的解决方法。我还没试过。

我最后做的事,也许能帮到别人。

后端(节点/快递/打印稿)

像往常一样设置cookie

res.status(200).cookie("token", token, cookieOptions)

为localhost做一个工作

// if origin localhost
response.setHeader("X-Set-Cookie", response.getHeader("set-cookie") ?? "");

在cors中允许x-set-cookie头

app.use(cors({
    //...
    exposedHeaders: [
        "X-Set-Cookie",
        //... 
    ]
}));

前端(Axios)

关于Axios响应 删除域=,所以它是默认的。 拆分多个cookie并存储在本地。

// Localhost cookie work around
const xcookies = response.headers?.["x-set-cookie"];
if(xcookies !== undefined){
    xcookies
        .replace(/\s+Domain=[^=\s;]+;/g, "")
        .split(/,\s+(?=[^=\s]+=[^=\s]+)/)
        .forEach((cookie:string) => {
            document.cookie = cookie.trim();
    });
}

不理想,但我可以继续我的生活了。

总的来说,我认为这太复杂了:-(

更新我的用例,也许我们可以解决它?

这是一个自定义域名的heroku服务器。 根据这篇文章,这应该是可以的 https://devcenter.heroku.com/articles/cookies-and-herokuapp-com

我做了一个孤立的测试用例,但仍然没有乐趣。 我很确定我以前在FireFox中见过它的工作,但目前似乎没有什么工作,除了我讨厌的工作。

服务器端

app.set("trust proxy", 1);

app.get("/cors-cookie", (request: Request, response: Response) => {

    // http://localhost:3000
    console.log("origin", request.headers?.["origin"]);

    const headers = response.getHeaders();
    Object.keys(headers).forEach(x => {
        response.removeHeader(x);
        
        console.log("remove header ", x, headers[x]);
    });
    console.log("headers", response.getHeaders());

    const expiryOffset = 1*24*60*60*1000; // +1 day

    const cookieOptions:CookieOptions = {
        path: "/",
        httpOnly: true,
        sameSite: "none",
        secure: true,
        domain: "api.xxxx.nl",
        expires: new Date(Date.now() + expiryOffset)
    }

    return response
        .status(200)
        .header("Access-Control-Allow-Credentials", "true")
        .header("Access-Control-Allow-Origin", "http://localhost:3000")
        .header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT")
        .header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept")
        .cookie("test-1", "_1_", cookieOptions)
        .cookie("test-2", "_2_", {...cookieOptions, ...{ httpOnly: false }})
        .cookie("test-3", "_3_", {...cookieOptions, ...{ domain: undefined }})
        .cookie("test-4", "_4_", {...cookieOptions, ...{ domain: undefined, httpOnly: false }})
        .cookie("test-5", "_5_", {...cookieOptions, ...{ domain: undefined, sameSite: "lax" }})
        .cookie("test-6", "_6_", {...cookieOptions, ...{ domain: undefined, httpOnly: false, sameSite: "lax" }})
        .cookie("test-7", "_7_", {...cookieOptions, ...{ domain: "localhost"}}) // Invalid domain
        .cookie("test-8", "_8_", {...cookieOptions, ...{ domain: ".localhost"}}) // Invalid domain
        .cookie("test-9", "_9_", {...cookieOptions, ...{ domain: "http://localhost:3000"}}) // Invalid domain
        .json({
            message: "cookie"
        });
});

客户端

const response = await axios("https://api.xxxx.nl/cors-cookie", {
    method: "get",
    withCredentials: true,
    headers: {
        "Accept": "application/json",
        "Content-Type": "application/json",                
    }
});

哪个会产生以下响应

我在网络>请求> cookie选项卡中看到cookie。

但是在Application > Storage > cookies和document.cookie中没有cookie。

这是对上面关于Heroku服务器的CORS cookie的“Lode Michels”的回答,(以及其他云提供商,如AWS)

你的CORS cookie无法设置的原因是因为Heroku剥离了负载均衡器的SSL证书,所以当你试图在服务器上设置“安全”cookie时,它会失败,因为它不再来自安全连接。

您可以显式地指定连接是否安全,而不是cookie模块检查请求。 https://github.com/pillarjs/cookies

用可可豆,加入这个:

ctx.cookies.secure = true;

编辑:我不能直接评论这个答案,因为低于50的声誉

注意2020年发布的Chrome浏览器。

未来的Chrome版本将只提供跨站点的cookie 请求,如果他们设置为SameSite=None和安全。

所以如果你的后端服务器没有设置SameSite=None, Chrome将默认使用SameSite=Lax,不会使用这个cookie {withCredentials: true}请求。

更多信息https://www.chromium.org/updates/same-site。

Firefox和Edge的开发人员也希望在未来发布这一功能。

Spec在这里找到:https://datatracker.ietf.org/doc/html/draft-west-cookie-incrementalism-01#page-8

皮姆的回答很有帮助, 但这是我经历过的一个边缘情况,

在我的情况下,即使我已经在BE中设置了访问控制-允许起源到特定的起源,在FE中,我收到它为*;这是不允许的

问题是,其他人负责网络服务器的设置, 其中,有一个配置来设置Access-Control-*头,这是覆盖我的头集从BE应用程序

唷. .花了一段时间才想明白。

因此,如果您设置的内容与您收到的内容不匹配,请检查您的web服务器配置。