获取ASP.NET中所有活动会话的列表

hhh*_*112 18 asp.net session

我知道用户使用以下代码行登录的用户:

Session["loggedInUserId"] = userId;
Run Code Online (Sandbox Code Playgroud)

我的问题是如何知道用户登录的内容,以便其他用户可以看到当前登录的用户.

换句话说,我可以获得所有活跃的"loggedInUserId"会话吗?

hhh*_*112 21

我没有尝试rangitatanz解决方案,但我使用了另一种方法,它对我来说效果很好.

private List<String> getOnlineUsers()
    {
        List<String> activeSessions = new List<String>();
        object obj = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
        object[] obj2 = (object[])obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj);
        for (int i = 0; i < obj2.Length; i++)
        {
            Hashtable c2 = (Hashtable)obj2[i].GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj2[i]);
            foreach (DictionaryEntry entry in c2)
            {
                object o1 = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Value, null);
                if (o1.GetType().ToString() == "System.Web.SessionState.InProcSessionState")
                {
                    SessionStateItemCollection sess = (SessionStateItemCollection)o1.GetType().GetField("_sessionItems", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(o1);
                    if (sess != null)
                    {
                        if (sess["loggedInUserId"] != null)
                        {
                            activeSessions.Add(sess["loggedInUserId"].ToString());
                        }
                    }
                }
            }
        }
        return activeSessions;
    }
Run Code Online (Sandbox Code Playgroud)

  • 好帖子.我在行`object [] obj2 =(object [])obj.GetType()得到一个错误.GetField("_ caches",BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj);`在iis6中(Windows服务器) 2003).在Windows XP和Windows 7中正常工作. (4认同)
  • 请尝试使用有意义的变量名称... obj 和 obj2 不要削减它;) (2认同)

Dav*_*ker 6

此页面列出了一个解决方案列出所有活动的 ASP.NET 会话

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

public static List<string> Sessions
{
        get
        {
            lock (padlock)
            {
                if (_sessionInfo == null)
                {
                    _sessionInfo = new List<string>();
                }
                return _sessionInfo;
            }
        }
  }

    protected void Session_Start(object sender, EventArgs e)
    {
        Sessions.Add(Session.SessionID);
    }
    protected void Session_End(object sender, EventArgs e)
    {
        Sessions.Remove(Session.SessionID);
    }
Run Code Online (Sandbox Code Playgroud)

基本上,它只是将会话跟踪到一个列表中,您可以使用它来查找相关信息。确实可以将任何您真正想要的内容存储到其中 - 用户名或其他任何内容。

我不认为 ASP .net 层有任何东西可以做到这一点?

  • 只是一点提醒:IIRC Session_End 事件仅在使用 InProcess 会话状态时引发。当使用 StateServer 或 SQL Server 作为会话状态时,情况并非如此,这意味着对象永远不会从 _sessionInfo 集合中删除。因此,您必须实施一些“超时”机制来删除超时会话。 (7认同)
  • 我的理解是“List.Add/Remove”不是线程安全的。那么这些操作不需要“上锁(挂锁)”吗? (2认同)