4

I have a WebApp that I am building with .net in C#. I want to see a list of all the online sessions in the site. So i wrote this in the global.asax page:

protected void Session_Start(object sender, EventArgs e)
{
    Application.Lock();
    List<string[]> onlineUsers = Application["onlineUsers"] as List<string[]>;
    string[] onlineUser = {Session.SessionID.ToString(), ""};
    onlineUsers.Add(onlineUser);
    Application.UnLock();
}

protected void Session_End(object sender, EventArgs e)
{
    Application.Lock();
    List<string[]> onlineUsers = Application["onlineUsers"] as List<string[]>;
    string[] toRemove = null;
    foreach(string[] user in onlineUsers){
       if(user[0] == Session.SessionID.ToString()){
          toRemove = user;
          break;
          }
       }
       if (toRemove != null)
          onlineUsers.Remove(toRemove);
    Application["onlineUsers"] = onlineUsers;
    Application.UnLock();
}

and then when the user is logging in i am calling to this function:

public static void afterLogIn(string userName)
{
   HttpContext.Current.Application.Lock();
   List<string[]> onlineUsers = HttpContext.Current.Application["onlineUsers"] as List<string[]>;
   foreach (string[] user in onlineUsers)
   {
      if (user[0] == HttpContext.Current.Session.SessionID.ToString())
      {
         user[1] = userName;
         break;
      }
    }
    HttpContext.Current.Application["onlineUsers"] = onlineUsers;
    HttpContext.Current.Application.UnLock();
}

so after it i have a list in aplication["onlineUsers"] than contains all the userNames and Ids. the problem is that i see only the sessions that connect from my computer, i dont see all the other users that connected, whats wrong?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Jordan
  • 570
  • 1
  • 6
  • 24

0 Answers0