简体   繁体   English

如何从请求中获取sessionId?

[英]How can I get sessionId from a request?

I use express to support my session management: 我用express来支持我的会话管理:

app.use(express.cookieParser('your secret here'));
app.use(express.session({secret: 'mysecretcode'}));

I what to extract and save the sessionId from the request. 我从请求中提取并保存sessionId的内容。 However, the id I get from req.sessionID is different to that sent in the cookie: 但是,我从req.sessionID获得的id与cookie中发送的id不同:

req.sessionID --> 'E7oSoKmQfcMKnk5_jA5tF5vR'
cookie.connect.sid --> 's%3AE7oSoKmQfcMKnk5_jA5tF5vR.DQnYdDDcFn8K2JJHMgWL5DTzNYYwIU3DA5a10WImA7U';
  1. Why these two are different? 为什么这两个是不同的?
  2. How can I get connect.sid before sending a response? 如何在发送响应之前获取connect.sid?

I read the sourcecode of connect.session, and got: 我读了connect.session的源代码,得到了:

key = options.key || 'connect.sid'

and

var val = 's:' + signature.sign(req.sessionID, secret);
val = cookie.serialize(key, val);
debug('set-cookie %s', val);
res.setHeader('Set-Cookie', val);

so, connect will set cookie into response header when response's 'header' event fired, and when you call response.end(), connect will save session data into store. 所以,当响应的'header'事件被触发时,connect会将cookie设置为响应头,当你调用response.end()时,connect会将会话数据保存到store中。

That's it. 而已。

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

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