本文介绍了获取ASP.NET中的所有活动会话列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道什么是用户与下面的行code的登录:
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中的所有活动会话列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!