using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using System.Collections; namespace Do.NET.Web { //web.config //Do.NET.Web.CurrentUserModule.Count gibt die Anzahl der Benutzer im System /* */ public class CurrentUserModule : IHttpModule { #region Props static TimeSpan _deleteSpan = new TimeSpan(0, 5, 0);//5 min static Hashtable _sessions = new Hashtable(10000); #endregion public static int Count { get { int counter = 0; //HttpContext context = HttpContext.Current; try { lock(_sessions) { //CurrentUserModule mod = context.ApplicationInstance.Modules.Get("CurrentUserModule") as CurrentUserModule; counter = _sessions.Count; } } catch (Exception ex) { throw new NotSupportedException("Module name CurrentUserModule not found", ex); } return counter; } } #region IHttpModule Members public void Dispose() { _sessions.Clear(); } public void Init(HttpApplication context) { context.AcquireRequestState += new EventHandler(context_AcquireRequestState); } #endregion void context_AcquireRequestState(object sender, EventArgs e) { lock (_sessions) { if (HttpContext.Current.Session == null) return; string key = HttpContext.Current.Session.SessionID; if (_sessions.ContainsKey(key)) { _sessions[key] = DateTime.Now.Add(_deleteSpan); } else {//new User //Delete user by deleteSpan if (_sessions.Count > 0) { try { foreach (string tmp_key in _sessions.Keys) { DateTime? time = _sessions[tmp_key] as DateTime?; if (time.HasValue) { if (time.Value < DateTime.Now) { _sessions.Remove(tmp_key); } } } } catch (Exception ex) { System.Diagnostics.Debug.Write(ex.ToString()); } } _sessions[key] = DateTime.Now.Add(_deleteSpan); } } } } }