列出所有活动的 ASP.NET 会话 [英] List all active ASP.NET Sessions

查看:24
本文介绍了列出所有活动的 ASP.NET 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何列出(并遍历)所有当前的 ASP.NET 会话?

How can I list (and iterate through) all current ASP.NET sessions?

推荐答案

您可以在 global.asax 事件 Session_Start 和 Session_End(仅在进程内设置中)中收集有关会话的数据:

You can collect data about sessions in global.asax events Session_Start and Session_End (only in in-proc settings):

private static readonly List<string> _sessions = new List<string>();
private static readonly object padlock = new object();

 public static List<string> Sessions
 {
       get
       {
            return _sessions;
       }
  }

  protected void Session_Start(object sender, EventArgs e)
  {
      lock (padlock)
      {
          _sessions.Add(Session.SessionID);
      }
  }
  protected void Session_End(object sender, EventArgs e)
  {
      lock (padlock)
      {
          _sessions.Remove(Session.SessionID);
      }
  }

你应该考虑使用一些并发集合来降低同步开销.ConcurrentBag 或 ConcurrentDictionary.或者 ImmutableList

You should consider use some of concurrent collections to lower the synchronization overhead. ConcurrentBag or ConcurrentDictionary. Or ImmutableList

https://msdn.microsoft.com/en-us/library/dd997373(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/dn467185.aspx

这篇关于列出所有活动的 ASP.NET 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆