Click here to Skip to main content
15,886,806 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to track users who have logged in using mvc
Posted

1 solution

The easiest way would be to add a static counter that would increment on Session_Start and decrement on Session_End. You'll need to add a variable (like Session["IsActive"] = true;) to the session to allow Session_end to fire properly.

If you want to track by-user you will need to add a static IEnumerable (or a class I suppose, if you prefer) that is similarly manipulated by the Session Start and end events, but using either names or your database keys.

C#
public class MvcApplication : HttpApplication
{
    public static List<int> UsersOnline{ get; set; } // or string, or custom class

    protected void ApplicationStart()
    {
        UsersOnline = new List<int>();
    }

    protected void Session_Start(object sender, EventArgs e)
    {
        UsersOnline.Add(/* Hook to DB or Directory here */);
    }

    protected void Session_Start(object sender, EventArgs e)
    {
        UsersOnline.Remove(/* Hook to DB or Directory here */);
    }
    ...
}
</int></int>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900