读取用户会话时出现NULL引用异常(反射)

时间:2012-11-19 07:00:24

标签: c# asp.net iis-6 hosting reflectionpermission

我已使用引用Reading All Users SessionGet 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相关的权限问题或信任级别设置有关吗?请让任何人遇到这样的问题。任何帮助都非常值得赞赏。

6 个答案:

答案 0 :(得分:3)

我尝试过Paully的解决方案,该解决方案在某些方面没有编译,导致其他方面的运行时错误。无论如何,受到他的建议的启发(非常感谢!我的投票就是这样),我来到了我自己,它编译并获得预期的数据。

另外,我正在返回一个IEnumerable,我正在使用&#34; yield return&#34;,这使得它对于大型列表(延迟加载数据)更具性能。在这里:

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;
            }
        }
    }
}

答案 1 :(得分:1)

我知道这是一个旧线程,但这可能会节省一些时间。要检查的另一件事是obj是System.Web.Caching.CacheMultiple类型。我有同样的问题,这是一个特定于平台的问题,正如Marc Gravell建议的那样。事实证明,在Windows 2003服务器上,obj是类型System.Web.Caching.CacheSingle,并且在尝试获取&#34; _caches&#34;的值时有一个空引用异常。

如果是这种情况,您仍然可以获得(Hashtable)obj.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(obj);

的活动会话列表

答案 2 :(得分:1)

如果_caches为NULL,请尝试使用_cachesRefs。下面的函数将返回所有多个Windows版本的所有用户会话集合,包括Windows Server。

有效。

public List<SessionStateItemCollection> GetAllUserSessions() {

List<Hashtable> hTables = new List<Hashtable>();

PropertyInfo propInfo = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static);

object CacheInternal = propInfo.GetValue(null, null);

dynamic fieldInfo = CacheInternal.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance);

if (fieldInfo != null) {
    object[] _caches = (object[])fieldInfo.GetValue(CacheInternal);
    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);
    }
} else {
    fieldInfo = CacheInternal.GetType().GetField("_cachesRefs", BindingFlags.NonPublic | BindingFlags.Instance);
    dynamic cacheRefs = fieldInfo.GetValue(CacheInternal);
    foreach (void cacheRef_loopVariable in cacheRefs) {
        cacheRef = cacheRef_loopVariable;
        dynamic target = cacheRef.Target;
        fieldInfo = target.GetType().GetField("_entries", BindingFlags.NonPublic | BindingFlags.Instance);
        Hashtable hTable = fieldInfo.GetValue(target);
        hTables.Add(hTable);
    }
}

List<SessionStateItemCollection> sessionlist = new List<SessionStateItemCollection>();

foreach (void hTable_loopVariable in hTables) {
    hTable = hTable_loopVariable;
    foreach (DictionaryEntry entry in hTable) {
        object value = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Value, null);
        if (value.GetType().ToString() == "System.Web.SessionState.InProcSessionState") {
            SessionStateItemCollection sCollection = (SessionStateItemCollection)value.GetType().GetField("_sessionItems", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(value);
            if (sCollection != null)
                sessionlist.Add(sCollection);
        }
    }
}

return sessionlist;

}

答案 3 :(得分:0)

听起来,2003年运行的.NET版本(或更新版本)与XP / Win7版本不同,尽管它也可能只是特定于平台的差异。如果是权限/信任,您会看到异常。相反,似乎更简单:_caches 不存在在2003机器上的任何版本上。如果你使用反射访问私有状态:你应该完全期望它在版本/更新/平台/ at-whim /等之间爆炸。

调查:

  • 检查obj是否为null
  • 检查obj.GetType().GetField("_caches", BindingFlags.NonPublic | BindingFlags.Instance)是否为null

(这些事情中的任何一个都可能导致你引用的这个例外)

答案 4 :(得分:0)

正是您的业务案例在asp.net中有Application状态变量。它类似于会话状态,但对所有用户请求都可见。

答案 5 :(得分:0)

截至2010年2月,在IIS 10.0(Windows 10)上进行测试,上述解决方案还没有完全奏效,因此我一起整理了以下对我有用的解决方案。它以awerdan的答案为基础,并结合了Diogo Damiani的答案。

    // attempt to get Asp.Net internal cache
    // adapted from https://stackoverflow.com/a/46554310/1086134
    private static object getAspNetInternalCacheObj ()
    {
        object aspNetCacheInternal = null;

        PropertyInfo cacheInternalPropInfo = typeof(HttpRuntime).GetProperty("CacheInternal", BindingFlags.NonPublic | BindingFlags.Static);
        if (cacheInternalPropInfo != null)
        {
            aspNetCacheInternal = cacheInternalPropInfo.GetValue(null, null);
            return aspNetCacheInternal;
        }

        // At some point, after some .NET Framework's security update, that internal member disappeared.
        // https://stackoverflow.com/a/45045160
        // 
        // We need to look for internal cache otherwise.
        //
        var cacheInternalFieldInfo = HttpRuntime.Cache.GetType().GetField("_internalCache", BindingFlags.NonPublic | BindingFlags.Static);
        if (cacheInternalFieldInfo == null)
            return null;

        var httpRuntimeInternalCache = cacheInternalFieldInfo.GetValue(HttpRuntime.Cache);
        var httpRuntimeInternalCacheField = httpRuntimeInternalCache.GetType().GetField("_cacheInternal", BindingFlags.NonPublic | BindingFlags.Instance);
        if (httpRuntimeInternalCacheField == null)
            return null;

        aspNetCacheInternal = httpRuntimeInternalCacheField.GetValue(httpRuntimeInternalCache);
        return aspNetCacheInternal;
    }

    // adapted from https://stackoverflow.com/a/39422431/1086134
    private static IEnumerable<System.Web.SessionState.SessionStateItemCollection> getAllUserSessions()
    {
        List<Hashtable> hTables = new List<Hashtable>();
        object obj = getAspNetInternalCacheObj();
        if (obj == null)
            yield break;

        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")
                {
                    System.Web.SessionState.SessionStateItemCollection sess = (System.Web.SessionState.SessionStateItemCollection)o1.GetType().GetField("_sessionItems", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(o1);
                    if (sess != null)
                        yield return sess;
                }
            }
        }
    }