重新启动浏览器后,我的 cookie 不再保留

My cookies don't persist after i restart the browser

我正在使用cookieSession来处理会话事务。没问题,除非我重新启动浏览器,我的会话不会持续存在。我希望会话每次都保持活跃并且永不过期。

这是我的设置

app.use(cookieSession({
  name: 'xxxx',
  keys: ['xxxxx', 'xxxx'],
  cookie: { secure: true,
            httpOnly: true,
            domain: 'mydomain.com',

            expires: 9999999999999999999999999999999999999999 
          }
  })
); 

有什么建议吗?

查看 cookie-session readme, I don't think you need to nest your options in a cookie property. Also, expires takes a Date object; you should pass that value as maxAge instead if you wish to give it the number of milliseconds until the cookie expires. Keep in mind that all cookies expire per the cookie specification,所以您最好将过期时间设置在很远的将来。

试试这个:

app.use(cookieSession({
  name: 'xxxx',
  keys: ['xxxxx', 'xxxx'],
  secure: true,
  httpOnly: true,
  domain: 'mydomain.com',
  maxAge: 9999999999999999999999999999999999999999 
  })
); 

编辑:一些较旧的浏览器不支持 maxAge,因此 expires 可能更适合兼容性。另外,注意 maxAge 中的溢出;负值可能会导致浏览器立即或在浏览器关闭时使 cookie 过期。考虑使用 Number.MAX_SAFE_INTEGER.