简体   繁体   English

如何获取 Web 应用程序中的活动会话列表?

[英]How to get a list of active sessions in a web application?

I am trying to get a list of active session id's in my web application.我试图在我的 Web 应用程序中获取活动会话 ID 的列表。

I tried to manage a list in Global.asax.我试图在 Global.asax 中管理一个列表。 I add session id when Session_Start is fired, and remove session id when Session_End is fired.我在 Session_Start 被触发时添加会话 ID,并在 Session_End 被触发时删除会话 ID。

The problem is in cases when the user closes the browser or shut down the computer.问题出现在用户关闭浏览器或关闭计算机的情况下。 The Session_End is not fired, and the seesion still exists in the list. Session_End 没有被触发,并且该列表仍然存在。

Somebody has an idea how can I get the list?有人知道我怎样才能得到清单?

Thanks!谢谢!

There is no solution to this problem that avoids the complication you note.没有解决此问题的方法可以避免您注意到的复杂情况。 Unless there is some explicit way to end a user's session - and the user activates it - the only way to know when a session has ended is to allow it to expire.除非有某种明确的方式来结束用户的会话 -并且用户激活它- 知道会话何时结束的唯一方法是允许它过期。

Session_End will eventually fire, when the session times out. Session_End最终会在会话超时时触发。 The only time you never see Session_End called is when the application domain itself unexpectedly quits.从未看到Session_End被调用的唯一时间是应用程序域本身意外退出时。 In such a case, you know that all of your sessions are dead, anyway.在这种情况下,您知道您的所有会话都已死,无论如何。

Andrew Barber is correct.安德鲁·巴伯是对的。 There is just one more thing you can do:您还可以做一件事:

  • Ask your users to logout.要求您的用户注销。 In fact not just ask them but in a way force/remind them that they have to log out.事实上,不仅要问他们,还要以某种方式强迫/提醒他们必须注销。

Here is how i did it:这是我如何做到的:

<script type="text/javascript">
    $(function () {
        $("a").click(function () {
            window.onbeforeunload = null;
        });
    }); 
</script>

<script type="text/javascript">
    window.onbeforeunload = confirmExit;
    function preConfirm() {
        if ((window.event.clientX < 0) || (window.event.clientY < 0)) {
            confirmExit();
        }
    }
    function confirmExit() {
        return "You have attempted to leave this page.  It is suggested to log out using the link.  Are you sure you want to exit this page?";
    }
</script>

By adding these scripts to your master page (if that is the case or in every page) the browser prompts the user with a modal window with your message if they try to load another page or close the browser without logging off.通过将这些脚本添加到您的母版页(如果是这种情况或在每个页面中),如果用户尝试加载另一个页面或关闭浏览器而不注销,浏览器会使用带有您的消息的模式窗口提示用户。

Of course the user my decide to leave the page anyway even though you remind him to log off but really you have tried your best!!当然,即使您提醒他注销,用户我还是决定离开该页面,但实际上您已尽力而为!!

If you use a SQL Server database to store your sessions in it becomes easier to retrieve what appears to be currently active by using an SQL query to get what you need.如果使用SQL Server数据库来存储您的会话过程变得更容易检索这似乎通过使用SQL查询来获取你所需要的是被激活的。

I say appears because like others have noted sessions can hang around if they are not properly cleared using Session.Abandon() eg if the user just closes their browser window without logging out.我说出现是因为就像其他人指出的那样,如果使用Session.Abandon()没有正确清除会话,则会话可能会挂起,例如,如果用户只是关闭浏览器窗口而不注销。 This means that you can only rely on the expiry date of the session to be sure the session is completely finished with, however long you have set that to be.这意味着您只能依靠会话的到期日期来确保会话完全结束,无论您设置了多长时间。

I should also point out that if you do use a database as your session store, the Session_End event will never fire.我还应该指出,如果您确实使用数据库作为会话存储,则Session_End事件将永远不会触发。

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

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