列出所有活动的ASP.NET会话

时间:2009-09-24 08:04:46

标签: asp.net session session-state

如何列出(并遍历)所有当前的ASP.NET会话?

9 个答案:

答案 0 :(得分:33)

您可以在global.asax事件Session_Start和Session_End中收集有关会话的数据(仅在进程内设置中):

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

 public static List<string> Sessions
 {
       get
       {
            return _sessions;
       }
  }

  protected void Session_Start(object sender, EventArgs e)
  {
      lock (padlock)
      {
          _sessions.Add(Session.SessionID);
      }
  }
  protected void Session_End(object sender, EventArgs e)
  {
      lock (padlock)
      {
          _sessions.Remove(Session.SessionID);
      }
  }

您应该考虑使用一些并发集合来降低同步开销。 ConcurrentBag或ConcurrentDictionary。或者ImmutableList

https://msdn.microsoft.com/en-us/library/dd997373(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/dn467185.aspx

答案 1 :(得分:19)

http://weblogs.asp.net/imranbaloch/archive/2010/04/05/reading-all-users-session.aspx

  

如何运作:

     

InProc会话数据存储在HttpRuntime的内部缓存中,实现了ICollection的ISessionStateItemCollection实现。在这段代码中,首先我获得了HttpRuntime类的CacheInternal静态属性,然后在这个对象的帮助下,我得到了_entries私有成员,它是ICollection类型。然后简单地枚举这个字典,只获取System.Web.SessionState.InProcSessionState类型的对象,最后得到每个用户的SessionStateItemCollection。

     

摘要:

     

在本文中,我将向您展示如何获取所有当前用户会话。但是,执行此代码时您会发现一件事是它不会显示当前请求上下文中设置的当前用户会话,因为会话将在所有Page事件后保存...

答案 2 :(得分:17)

似乎没有任何类或方法提供此信息。我认为,拥有SessionStateStoreProvider功能是一件很好的事情,有一个返回当前活动会话的方法,这样我们就不必像Jan Remunda所提到的那样主动跟踪session_start和session_end中的会话生命。

由于我找不到任何开箱即用的方法来获取所有会话列表,而且我不想跟踪Jan提到的会话生活,我最终得到了这个解决方案,这在我的案例中起作用。

public static IEnumerable<SessionStateItemCollection> GetActiveSessions()
{
    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)
                {
                    yield return sess;
                }
            }
        }
    }
}

答案 3 :(得分:4)

我真的很喜欢ajitdh的回答。请他投票。以下是该解决方案的另一个参考:

http://weblogs.asp.net/imranbaloch/reading-all-users-session

这让我很接近,但它未能达到我个人的目标找到我知道的特定会话ID 的会话。所以,为了我的目的,我只是将sessionid添加为会话项(比如Session [&#34; SessionId&#34;] = session.SessionId on Session Start。)然后我只是寻找一个具有匹配值的会话。我本来希望通过索引到其中一个集合来实际提取这个条目,但这确实让它至少起作用。

当然,这就是In-Proc会议的全部内容,我确实正在考虑离开。

private static SessionStateItemCollection GetSession(string sessionId)
{
    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 o0 = entry.Value.GetType().GetProperty("Value", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(entry.Key, null);
            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["SessionId"] != null && ((string)sess["SessionId"]) == sessionId)
                    {
                        return sess;
                    }
                }
            }
        }
    }

    return null;
}

答案 4 :(得分:1)

这是一个WebForms / Identity示例,它获取在过去30分钟内处于活动状态的登录用户的列表。

以下答案适用于单个Web服务器,如果应用程序重新启动,缓存将丢失。如果要保留数据并在Web场中的服务器之间共享,可能会感兴趣this answer

在Global.asa.cs中:

public static ActiveUsersCache ActiveUsersCache { get; } = new ActiveUsersCache();

protected void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
    if (User != null && User.Identity.IsAuthenticated)
    {
        // Only update when the request is for an .aspx page
        if (Context.Handler is System.Web.UI.Page)
        {
            ActiveUsersCache.AddOrUpdate(User.Identity.Name);
        }
    }
}

添加这几个类:

public class ActiveUsersCache
{
    private readonly object padlock = new object();
    private readonly Dictionary<string, DateTime> cache = new Dictionary<string, DateTime>();
    private DateTime lastCleanedAt;

    public int ActivePeriodInMinutes { get; } = 30;
    private const int MinutesBetweenCacheClean = 30;

    public List<ActiveUser> GetActiveUsers()
    {
        CleanCache();

        var result = new List<ActiveUser>();
        lock (padlock)
        {
            result.AddRange(cache.Select(activeUser => new ActiveUser {Name = activeUser.Key, LastActive = activeUser.Value}));
        }
        return result;
    }

    private void CleanCache()
    {
        lastCleanedAt = DateTime.Now;
        var cutoffTime = DateTime.Now - TimeSpan.FromMinutes(ActivePeriodInMinutes);
        lock (padlock)
        {
            var expiredNames = cache.Where(au => au.Value < cutoffTime).Select(au => au.Key).ToList();
            foreach (var name in expiredNames)
            {
                cache.Remove(name);
            }
        }
    }

    public void AddOrUpdate(string userName)
    {
        lock (padlock)
        {
            cache[userName] = DateTime.Now;
        }

        if (IsTimeForCacheCleaup())
        {
            CleanCache();
        }
    }

    private bool IsTimeForCacheCleaup()
    {
        return lastCleanedAt < DateTime.Now - TimeSpan.FromMinutes(MinutesBetweenCacheClean);
    }
}

public class ActiveUser
{
    public string Name { get; set; }

    public DateTime LastActive { get; set; }

    public string LastActiveDescription
    {
        get
        {
            var timeSpan = DateTime.Now - LastActive;
            if (timeSpan.Minutes == 0 && timeSpan.Seconds == 0)
                return "Just now";
            if (timeSpan.Minutes == 0)
                return timeSpan.Seconds + "s ago";
            return $"{timeSpan.Minutes}m  {timeSpan.Seconds}s ago";
        }
    }
}

最后,在要显示活动用户的页面中,显示结果:

UserRepeater.DataSource = Global
    .ActiveUsersCache.GetActiveUsers().OrderByDescending(u => u.LastActive);
UserRepeater.DataBind();

答案 5 :(得分:1)

我一直在寻找与ASP.net更高版本相同的@ajitdh答案-找不到任何东西,因此以为我会使用v4.6.2的解决方案更新此线程...还没有已在.net的更高版本中进行了测试,但不适用于v4.5。我猜它将与v4.6.1及更高版本兼容。

Cache cache = HttpRuntime.Cache;
MethodInfo method = typeof( System.Web.Caching.Cache ).GetMethod( "GetInternalCache", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.FlattenHierarchy );

object objx = method.Invoke( cache, new object[] { false } );

FieldInfo field = objx.GetType().GetField( "_cacheInternal", BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance );
objx = field.GetValue( objx );

field = objx.GetType().GetField( "_cachesRefs", BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance );
objx = field.GetValue( objx );

IList cacherefs = ( (IList)objx );
foreach( object cacheref in cacherefs )
{
    PropertyInfo prop = cacheref.GetType().GetProperty( "Target", BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Instance );

    object y = prop.GetValue( cacheref );

    field = y.GetType().GetField( "_entries", BindingFlags.NonPublic | BindingFlags.FlattenHierarchy | BindingFlags.Instance );

    Hashtable c2 = (Hashtable)field.GetValue( y );
    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 )
            {
                // Do your stuff with the session!
            }
        }
    }
}

答案 6 :(得分:0)

据我所知,使用标准的内存会话是不可能的。这是我上周要解决的问题,我确定除非你使用会话状态服务器,否则它是不可能的。如果你问我,从设计的角度看似乎很奇怪。 :/

答案 7 :(得分:-1)

您也可以尝试一些基本的操作,但这只是为了您可以理解概念

protected void Page_Load(object sender, EventArgs e)
    {
        if (Convert.ToInt32(Session["islogged"]) == 0)
        {
            Label1.Text = "you are not logged";
        }

    }

    protected void logged(object sender, EventArgs e)

/ *将其添加到按钮中,以便在单击该按钮时会出现该按钮* /         {

            Label1.Text = "you are  Logged in "; 


    }

答案 8 :(得分:-16)

尝试以下代码

 for (int i = 0; i < Session.Keys.Count - 1; i++)
        {
            Label1.Text += Session.Keys.Get(i) + " - " + Session[i].ToString()+"<br/>";
        }