Introduction
As far as I know, in ASP.NET, we do not have a predefined class library to find out all the active session IDs and their data. This article helps you list all the active session keys using a simple logic.
Background
This code can be used to track the active sessions of an application.
Using the code
This simple application has two pages: one to view the data (Manager.aspx) and the other to enter the current date time into the session.
In the application, create a variable of type Dictionary<string, HttpSessionState>
and store it into the application memory. It facilitates us to keep the “Session IDs” of all the sessions and a “Session” object reference.
protected void Application_Start(object sender, EventArgs e)
{
Dictionary<string, HttpSessionState> sessionData =
new Dictionary<string, HttpSessionState>();
Application["s"] = sessionData;
}
Once we are ready with the object structure, we need to add and remove Session IDs and Session objects in the Session_Start
and Session_End
events, respectively. By default, ASP.NET reuses Session IDs, so we cross-check every time we add a Session ID reference.
protected void Session_Start(object sender, EventArgs e)
{
Dictionary<string, HttpSessionState> sessionData =
(Dictionary<string, HttpSessionState>)Application["s"];
if (sessionData.Keys.Contains(HttpContext.Current.Session.SessionID))
{
sessionData.Remove(HttpContext.Current.Session.SessionID);
sessionData.Add(HttpContext.Current.Session.SessionID,
HttpContext.Current.Session);
}
else
{
sessionData.Add(HttpContext.Current.Session.SessionID,
HttpContext.Current.Session);
}
Application["s"] = sessionData;
}
protected void Session_End(object sender, EventArgs e)
{
Dictionary<string, HttpSessionState> sessionData =
(Dictionary<string, HttpSessionState>)Application["s"];
sessionData.Remove(HttpContext.Current.Session.SessionID);
Application["s"] = sessionData;
}
We are done with our Global.asax.cs page coding; the next part is we need an interface to store some dummy values in to the Session and a page to monitor all the application objects. For this, we use a very simple code in the Default.aspx page to keep the session values.
Apart from this, the Manager.aspx.cs page contains code to see all session data. This page contains an AJAX timer control and uses a GridView
to display data. It refreshes every 10 seconds.
protected void button1_Click(object sender, EventArgs e)
{
HttpContext.Current.Session[Guid.NewGuid().ToString()] =
DateTime.Now.ToString();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
try
{
List<SessionData> gv = new List<SessionData>();
Dictionary<string, HttpSessionState> sessionData =
(Dictionary<string, HttpSessionState>)Application["s"];
if (sessionData != null)
{
foreach (KeyValuePair<string, HttpSessionState> item in sessionData)
{
gv.Add(new SessionData() { SessionId=item.Key, SessionKey =null,
SessionValue = null });
if (item.Value != null && item.Value.Count > 0)
{
foreach (string key in item.Value.Keys)
{
gv.Add(new SessionData() {SessionId=item.Key, SessionKey = key,
SessionValue = item.Value[key] != null ?
item.Value[key].ToString() : string.Empty });
}
}
}
sessionValuesGrid.DataSource = gv;
sessionValuesGrid.DataBind();
}
}
catch { }
}
Apart from this, we can use "Session.IsNewSession
" to detect whether a Session is new or an existing one. We have one another hard and robust technique as well, which is using HttpModules.
Points of interest
Using this kind of logic in production environment is very difficult. If Microsoft provides an alternative, that would be really helpful in tracking expiring sessions easily.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.