How to save and retrieve all Sessions of a user

1

I'm developing a system in which I need to recover all users logged in or logged on to the server.

Context: When the user logs in to the system, in addition to creating the session , I save the information with the logon date and session time in the database.

Session["exemplo"] = exemplo;

When the user logs off, I drop the session and change the information in the database. Following this structure, I can have all users logged in "in hands". The problem that occurs to me is when the user is idle and the session automatically drops or when the user closes the browser. That way I can no longer change the status of the same in the database.

I would like to know if there is a way to retrieve all the active sessions on a server, ie recover all the users that are still logged into the system and their sessions have not yet expired? p>

Has anyone ever done anything similar?

    
asked by anonymous 28.10.2014 / 19:34

2 answers

5

You can store the list of enabled sessions as an Application item. So:

public void Session_OnStart()
{
    if (Application["SessionList"] == null)
        Application["SessionList"] = new List<HttpSessionState>();

    var sl = (List<HttpSessionState>) Application["SessionList"];

    sl.Add(Session);

    Application["SessionList"] = sl;
}

Do the same, however, by removing the session, when the Session_OnEnd event occurs.

    
28.10.2014 / 19:41
0

Following the @OnoSendai line, you could use such code in your Global.asax:

public void Application_OnStart()
{
  Application["UsersOnline"] = 0;
}

public void Session_OnStart()
{
  Application.Lock();
  Application["UsersOnline"] = (int)Application["UsersOnline"] + 1;
  Application.UnLock();
}

public void Session_OnEnd()
{
  Application.Lock();
  Application["UsersOnline"] = (int)Application["UsersOnline"] - 1;
  Application.UnLock();
}
    
06.11.2014 / 04:18