c# – 读取用户会话时的NULL引用异常(反射)
作者:互联网
我已经使用参考Reading All Users Session和Get a list of all active sessions in ASP.NET实现了用于读取活动会话的代码.
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;
}
它在本地系统(Windows XP和Windows 7)中运行良好.当我在Windows Server 2003(IIS版本6)中托管应用程序时,它在行中提供了NULL对象引用错误
object[] obj2 = (object[])obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj);
这与IIS相关的权限问题或信任级别设置有关吗?请让任何人遇到这样的问题.任何帮助都非常值得赞赏.
解决方法:
我尝试过Paully的解决方案,它在某些方面没有编译,导致其他人的运行时错误.无论如何,受到他的建议的启发(非常感谢!我的投票就是这样),我来到了我自己,它编译并获得预期的数据.
此外,我正在返回一个IEnumerable,我正在使用“yield return”,这使得它对于大型列表(一种延迟加载数据)更具性能.在这里:
public static System.Collections.Generic.IEnumerable<SessionStateItemCollection> GetAllUserSessions()
{
List<Hashtable> hTables = new List<Hashtable>();
object obj = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static).GetValue(null, null);
dynamic fieldInfo = obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance);
//If server uses "_caches" to store session info
if (fieldInfo != null)
{
object[] _caches = (object[])fieldInfo.GetValue(obj);
for (int i = 0; i <= _caches.Length - 1; i++)
{
Hashtable hTable = (Hashtable)_caches[i].GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(_caches[i]);
hTables.Add(hTable);
}
}
//If server uses "_cachesRefs" to store session info
else
{
fieldInfo = obj.GetType().GetField("_cachesRefs", BindingFlags.NonPublic | BindingFlags.Instance);
object[] cacheRefs = fieldInfo.GetValue(obj);
for (int i = 0; i <= cacheRefs.Length - 1; i++)
{
var target = cacheRefs[i].GetType().GetProperty("Target").GetValue(cacheRefs[i], null);
Hashtable hTable = (Hashtable)target.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(target);
hTables.Add(hTable);
}
}
foreach (Hashtable hTable in hTables)
{
foreach (DictionaryEntry entry in hTable)
{
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)
yield return sess;
}
}
}
}
标签:c,asp-net,hosting,iis-6,reflectionpermission 来源: https://codeday.me/bug/20190520/1144154.html