express - Cookie “cookieName” 将很快被拒绝,因为它的 “sameSite” 属性设置为 “none” 或 clearCookie 上的无效值

标签 express firefox cookies

我有一个带有以下代码的快速 API 以设置 cookie:

...
res.setHeader("Access-Control-Allow-Origin", ip);
res.cookie(name, token, { maxAge: 30000, httpOnly: true, secure: false, sameSite: "Lax" });
...

另一个清除 cookie:

...
res.clearCookie(name);
...

当我执行第二个时,我收到以下错误消息: Cookie“cookieName”将很快被拒绝,因为它的“sameSite”属性设置为“none”或 clearCookie 上的无效值

一些细节:

  • 错误仅出现在 Firefox 中,不会出现在 Chrome 中。
  • 在设置 sameSite: "Lax"之前,我在设置 cookie 之后和清除它时收到了两次错误消息。将 sameSite 设置为 Lax 后,问题仅在清除 cookie 时出现。

有什么帮助吗?让我知道您是否需要更多详细信息、版本、代码...

最佳答案

您遇到了 HTTP 问题。一般规则是,对于设置或删除 cookie,您必须将所有参数设置为相同(值和过期时间除外)。答案 https://stackoverflow.com/a/20320610/941493 中描述了您问题的正确解决方案。在RFC 6265 .

Finally, to remove a cookie, the server returns a Set-Cookie header with an expiration date in the past. The server will be successful in removing the cookie only if the Path and the Domain attribute in the Set-Cookie header match the values used when the cookie was created.

clearCookie的文档中也是这样写的:

Web browsers and other compliant clients will only clear the cookie if the given options is identical to those given to res.cookie(), excluding expires and maxAge.

因此,在您的情况下,使用过去的 expires 调用 cookie 命令,或者使用用于创建 cookie 的相同参数调用 clearCookie

关于express - Cookie “cookieName” 将很快被拒绝,因为它的 “sameSite” 属性设置为 “none” 或 clearCookie 上的无效值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61932916/

相关文章:

node.js - 使用 Mongoskin 查找不同类型的 MongoDB 文档

express - webpack 无法导入图像(在 typescript 中使用 express 和 angular2)

firefox - 如何为 Firefox 设置插件商店图标

cookies - 使用 NodeJS 和 ExpressJS 为域而不是子域设置 cookie

jQuery cookie 过期值

node.js - 在expressjs 4.x中将less文件编译为css

javascript - 使用 elemMatch 从数组 Node.js 返回一个对象

javascript - 如何在 firefox sdk 中设置请求超时设置?

css - :target pseudo-class not working properly in firefox

python - Django session : changing session key when modified