【问题标题】:Redirect to a new subdomain and set cookie on the redirected subdomain重定向到新的子域并在重定向的子域上设置 cookie
【发布时间】:2020-05-14 13:55:40
【问题描述】:

快递:4.x

NodeJS:12.x

a.example.com 上,我有一个 GET 监听器,它重定向到 b.example.com 并在 b.example 上设置 cookie。 com

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');
})

但是 b.example.com 中没有设置任何 cookie。我在这里错过了什么?

【问题讨论】:

    标签: javascript node.js express cookies subdomain


    【解决方案1】:

    好的,所以我通过使用 res.cookie 选项找到了答案。如果有人遇到这个问题,这里是正确的配置。

    如果您的客户端 JavaScript 应用程序需要读取 cookie,请将 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');
    

    【讨论】:

      【解决方案2】:

      如果您在本地检查此内容,即在 http://localhost 上,您将看不到安全 cookie。

      【讨论】:

      • 这应该是评论而不是答案。顺便说一句,我不在本地主机上。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-14
      • 1970-01-01
      相关资源
      最近更新 更多