简体   繁体   English

当 SameSite 为 none 时,Express 不会设置 cookie

[英]Express doesn't set-cookie when SameSite is none

I have read a lot of other similar questions, but I couldn't solve the issue.我已经阅读了很多其他类似的问题,但我无法解决问题。

My setup is Node + Express + PassportJs and everything works in development, but I have problems on production.我的设置是 Node + Express + PassportJs,一切都在开发中,但我在生产中遇到了问题。

With the following code, I see that the session cookie is sent back in the response, but I also get a message saying that it won't be applied as SameSite is lax (the default) and the response comes from another site (frontend and backend do not have the same origin).使用以下代码,我看到会话 cookie 在响应中发回,但我也收到一条消息,说它不会被应用,因为SameSite是松散的(默认)并且响应来自另一个站点(前端和后端没有相同的来源)。

app.use( 
    session({ 
        secret: "foo", 
        resave: false, 
        saveUninitialized: false, 
        store: MongoStore.create({ mongoUrl: process.env.MONGO_DB_CONN_STRING! }), 
        cookie: { httpOnly: true }
    }) 
); 

在此处输入图像描述

So I changed it to this, so to specify SameSite and Secure in production, but at this point, no cookie is set anymore!所以我把它改成这样,以便在生产中指定SameSiteSecure ,但此时,不再设置 cookie!

app.use( 
    session({ 
        secret: "foo", 
        resave: false, 
        saveUninitialized: false, 
        store: MongoStore.create({ mongoUrl: process.env.MONGO_DB_CONN_STRING! }), 
        cookie: isProduction ? { httpOnly: true, sameSite: "none", secure: true } : {}  // <-- only change
    }) 
); 

在此处输入图像描述

What could be the cause?可能是什么原因? I've tried to fix it by playing with CORS (no success) and other 100 things.我试图通过玩 CORS(没有成功)和其他 100 件事来修复它。 Yet it seems some quirk I am missing.然而,我似乎缺少一些怪癖。

depending on what service you use to deploy your API(netlify, render.com, heroku other...) you have to enable proxy根据您使用什么服务来部署您的 API(netlify、render.com、heroku 其他...),您必须启用代理

 this.app.enable('trust proxy');

it fixed my issue它解决了我的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法在 Nodejs (Express) 中将 cookie 的 SameSite 属性设置为 None - I can't set the SameSite attribute of the cookie to None in Nodejs (Express) Express 不设置 cookie - Express doesn't set a cookie Express JS/Node JS:当secure=true,sameSite:'none'时,浏览器没有设置cookie - Express JS/ Node JS : Browsers are not setting cookie when secure=true, sameSite: 'none' 如何使用 express 在 cookie 中设置 sameSite 和 Secure 属性? - How to set sameSite and Secure attributes in a cookie with express? 将 sameSite 从严格或松散设置为无时出错 - HTTP 仅 cookie - ExpressJS - Error when set sameSite from strict or lax to none - HTTP only cookie - ExpressJS res.setHeader(“ Set-Cookie”,…)未在Node / Express中设置cookie - res.setHeader(“Set-Cookie”, …) is not setting the cookie in Node / Express React/Express set-cookie 不能跨不同的子域工作 - React/Express set-cookie not working across different subdomains Express:禁用特定路由的标头(Etag、set-cookie 等) - Express: Disable headers for specific routes (Etag, set-cookie, etc) 使用 res.json 时,Express JS 设置 cookie 不起作用 - Express JS set a cookie doesn't work when using res.json Cookie设置在位置“ / set-cookie”,但无法使用express.js在“ / user”处检索。 - A cookie is set at location `/set-cookie` but cannot be retrieved at `/user` using express.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM