简体   繁体   English

如何根据SessionID恢复会话?

[英]How do I recover a Session based on the SessionID?

Context is: I need to send the SessionID (will be encripted) to an app, then the app saves the session ID and on the next query the app sends me back the SessionID so I can use the stored Session variables. 上下文是:我需要将SessionID(将被加密)发送给应用程序,然后该应用程序保存该会话ID,并在下一次查询时,该应用程序将我的SessionID发送回给我,以便我可以使用存储的Session变量。

So HttpContext.Current.Session.SessionID gives me the current SessionID. 所以HttpContext.Current.Session.SessionID给了我当前的SessionID。

But... how do I recover the Session based on the SessionID? 但是...如何根据SessionID恢复会话?

PS I am aware that this is a duplicate of this unanswered question: How do I get a session by SessionID in C# so as a plus if there is no way of retrieving the Session from the SessionID I would ask if there is another way to achieve that. PS我知道这是一个未解决问题的重复: 我如何在C#中通过SessionID获取会话,所以如果没有办法从SessionID检索Session,那么我会问是否还有另一种方法可以实现那。

As far as I am aware, there is no public API available to load a session based on session ID (I have asked the question here , as well). 据我所知,尚无可用于基于会话ID加载会话的公共API(我也在这里提出了问题 )。 You can hack it using reflection , but that will only work if your site is in a full trust environment and you are okay with the performance implications of using reflection. 您可以使用反射破解它 ,但是只有在您的站点处于完全信任的环境中并且您可以确定使用反射对性能的影响时,这种方法才能起作用。

As was pointed out in the comments, session state is normally driven by a cookie (by default named "ASP.NET_SessionId"). 如评论中所指出的,会话状态通常由cookie驱动(默认名称为“ ASP.NET_SessionId”)。 If you pass the cookie from the request of your "app" (which I am assuming you mean a remote application) to the server, you can then access session state simply by calling HttpContext.Current.Session on the server side. 如果将cookie从“应用程序”(我假设您是指远程应用程序)的请求传递到服务器,则只需在服务器端调用HttpContext.Current.Session即可访问会话状态。 You just need to add the cookie to the headers of the request from the application for it to work. 您只需要将Cookie添加到应用程序的请求标头中即可使其正常工作。

However, I don't think session state is the persistence mechanism you are after for your scenario. 但是,我不认为会话状态是您针对场景所追求的持久性机制。 Rather than dealing with cookies, it would make more sense to store your state in a persistent format such as a file or a database and use a GUID or other unique identifier to access it based on an ID passed in through the URL. 与其处理Cookie,不如将状态存储为文件或数据库之类的持久格式,并根据通过URL传递的ID使用GUID或其他唯一标识符来访问它,这更有意义。

As Alexi pointed out, another possibility would be to build your own session state provider that you put your own back door into in order to access the raw data, but it will take quite a bit of work to pull that off. 正如Alexi所指出的,另一种可能性是建立自己的会话状态提供程序 ,您可以将其放入自己的后门以访问原始数据,但是要实现这一目标需要大量的工作。

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

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