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

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

您可以在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

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

这是如何工作的:

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

总结:

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

没有任何提供这些信息的类别或方法似乎是不对的。 我认为,为SessionStateStoreProvider提供一个特性是一个很好的方法,它有一个返回当前活动会话的方法,所以我们不必像Jan Remunda提到的那样,在session_start和session_end中主动跟踪会话生命。

由于我找不到任何开箱即用的方法来获取所有会话列表,而且我也不想跟踪Jan提到的会话生活,所以我最终得到了这个解决scheme,这个解决scheme对我来说很合适。

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

我真的很喜欢ajitdh的答案。 做好他的工作。 这是另一个解决scheme的参考:

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

这使我接近了,但是我没有实现我个人的目标,即find我所知道的特定会话ID的会话 。 所以,为了我的目的,我只是将sessionid添加为会话项目(在会话开始时说Session [“SessionId”] = session.SessionId。)然后,我只查找具有匹配值的会话…我会更喜欢实际上通过索引到其中一个集合中来取出这个条目,但是这确实至less起到了作用。

当然,这是所有在进程中的会议,我确实正在考虑离开。

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

据我所知,使用标准的内存会话是不行的。 这是我上周正在处理的事情,我决定,除非你使用会话状态服务器,否则这是不可能的。 如果你问我的话,从deviseangular度看似乎很奇怪。 :/

以下是一个WebForms / Identity示例,可获取在过去30分钟内处于活动状态的已login用户的列表。

下面的答案适用于单个Web服务器,如果应用程序重新启动,caching将丢失。 如果你想坚持数据并在Web场中的服务器之间共享, 这个答案可能是有意义的。

在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(); 

尝试下面的代码

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