简体   繁体   English

重定向到新的子域并在重定向的子域上设置 cookie

[英]Redirect to a new subdomain and set cookie on the redirected subdomain

Express: 4.x快递:4.x

NodeJS: 12.x节点JS:12.x

On a.example.com I have a GET listener that redirects to b.example.com and sets a cookie on b.example.com .a.example.com 上,我有一个GET侦听器,它重定向到b.example.com并在b.example.com上设置一个 cookie。

app.get('/foo', (req, res) => {
    res.cookie('name', 'foo', { domain: '.example.com', path: '/', secure: true })
    res.cookie('age', '21', { domain: '.example.com', path: '/', secure: true })
    res.redirect(302, 'https://b.example.com');
})

But none of the cookies are set in b.example.com .但是在 b.example.com 中没有设置cookies What am I missing here?我在这里想念什么?

Ok, So I figured out the answer by playing around with the res.cookie options.好的,所以我通过使用 res.cookie 选项找到了答案。 If anybody comes upon this, here is the right configuration.如果有人遇到这个,这里是正确的配置。

Set the httpOnly flag to false if your client javascript application is required to read the cookies.如果您的客户端 javascript 应用程序需要读取 cookies,请将 httpOnly 标志设置为 false。

res.cookie('user', 'foo', { path: '/',  domain: '.example.com',  httpOnly:false secure: false  })
res.cookie('age', '21', { path: '/',  domain: '.example.com', httpOnly:false, secure: false  })
res.redirect('https://b.example.com');

If you are checking for this locally, ie on http ://localhost, you wont see secure cookies.如果你在本地检查这个,即在http ://localhost,你不会看到安全的 cookies。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM