获取 ASP.NET 中所有活动会话的列表 [英] Get a list of all active sessions in ASP.NET
本文介绍了获取 ASP.NET 中所有活动会话的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道使用以下代码行登录的是哪个用户:
I know what user is logged in with the following line of code:
Session["loggedInUserId"] = userId;
我的问题是如何知道哪些用户登录了,以便其他用户可以看到当前登录的用户.
The question I have is how do I know what users are logged in so that other users can see what users are currently logged in.
换句话说,我可以获取所有活动的loggedInUserId"会话吗?
In other words can I get all "loggedInUserId" sessions that are active?
推荐答案
我没有尝试 rangitatanz 解决方案,但我使用了另一种方法,它对我来说效果很好.
I didn't try rangitatanz solution, but I used another method and it worked just fine for me.
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;
}
这篇关于获取 ASP.NET 中所有活动会话的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文