hi atul,
you can get the number of active sessions via the performance monitoring
component of windows.
however if you want to track data in the sessions, that you are restricted
to .Net as far as i am aware. i haven't heard of a built-in method to list
active sessions and usernames etc in .Net 2.0.
i've included below the code i use to track user sessions. it's quite
simple, my only requirement is to record who is online, and when they last
accessed a page. I use the Cache object to track sessions because it has
built-in expiry functionality which is needed in cases where the user closes
the browser (instead of clicking 'log-out) and the session won't expire
until the time-out occurs, at which time you no longer have access to the
data in the session, so you can't track who the session belonged to. the
Cache takes care of removing entries automatically after a specified idle
period.
global.asax:
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
// update the 'CurrentUser' cached entry for this user
if(Context.User != null && Context.User.Identity != null &&
Context.User.Identity.IsAuthenticated)
{
string key = String.Format("Cache_{0}", Context.User.Identity.Name);
Context.Cache.Remove(key); // just in case it's there already, cache
allows duplicate names
Context.Cache.Add(key, new ObjectPair(User.Identity.Name,
DateTime.Now.ToString("HH:mm:ss")), null, DateTime.Now.AddMinutes(15),
Cache.NoSlidingExpiration, CacheItemPriority.Normal, null);
}
}
Logout.aspx:
private void Page_Load(object sender, System.EventArgs e)
{
string key = String.Format("Cache_{0}", User.Identity.Name);
Context.Cache.Remove(key); // remove from active users list
Active_Users_List.aspx:
private void Page_Load(object sender, System.EventArgs e)
{
this.DataGrid1.DataSource = ActiveUsers;
this.DataGrid1.DataBind();
}
/// <summary>
/// Returns an array of Pair objects, containing the username (First) and
the last access time (Second)
/// </summary>
public static ArrayList ActiveUsers
{
get
{
ArrayList activeUsers = new ArrayList();
IDictionaryEnumerator CacheEnum =
HttpContext.Current.Cache.GetEnumerator();
while (CacheEnum.MoveNext())
{
if(CacheEnum.Current is DictionaryEntry)
{
DictionaryEntry de = (DictionaryEntry)CacheEnum.Current;
if(de.Key.ToString().StartsWith("Cache_"))
{
// the value stores the username and the last access time in a Pair
object
activeUsers.Add(de.Value as ObjectPair);
}
}
}
return activeUsers;
}
}
Last but not least, the ObjectPair class to store the Username/AcessTime
values. there is one in the framework already but it can't be used in a
GridView because it doesn't have properties, only public members. You could
probably use a DictionaryList instead but i vaguely recall some limitations
there.
/// <summary>
/// A class to store a pair of objects. There is a class in System.Web.UI
but this class has fields
/// and not properties, which are necessary for using with a gridview.
/// </summary>
public class ObjectPair
{
private object pFirst, pSecond;
public ObjectPair(object f, object s)
{
this.pFirst = f;
this.pSecond = s;
}
public object First
{
get { return pFirst; }
set { pFirst = value; }
}
public object Second
{
get { return pSecond; }
set { pSecond = value; }
}
}
hope this helps
tim