开发者

count number of sessions in ASP.net/c#

I need to count number of sessions but it dose not work when I 开发者_开发知识库say if numbers of sessions are 2 then do something. The example below is my code:

  // count curent session in order to keep two player
  if (HttpContext.Current.Session.Count == 2)
  {
     Response.Redirect("update.aspx");
  }

I place the above code in code behind. is there any other way that I can say: if number of sessions are 2 else do something...


This is the count of session variables stored in the session for that user (msdn reference)...not the number of user sessions that exist currently.

You will need to store the session count outside of the session itself...perhaps in the Cache or Application cache.

Here are some SO questions to help implement this:

  • How to count sessions in asp.net server application
  • Determining an 'active' user count of an ASP.NET site


NOTE: this example is just for a novice programmer (NOT for ASP expert programers)

1) Go to Global.asax.cs file and identify the application startup function and then add a Session counter variable. Like this...

    void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup                                       
        Application.Add("NOF_USER_SESSION", 0);

2) Then in the same GLobal.asax.cs file keep adding/reducing the user counts in Session-Startup and Session-Endup function respectively... like this...

     void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Application["NOF_USER_SESSION"] =          (int)Application["NOF_USER_SESSION"] + 1;
..
..
        void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends. 
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer 
            // or SQLServer, the event is not raised.
            Application["NOF_USER_SESSION"] = (int)Application["NOF_USER_SESSION"] - 1;
..
..

3) Then use this Application level variable (int)Application["NOF_USER_SESSION"] wherever you can inside your program.


I've found the Session_Start Session_End somewhat unreliable, Session_End sometimes does not seem to be called. This is what I use, it maintains a dictionary of client IP address and last access date, timing out "sessions" after 20 minutes. Here I store the count in a static property called NumberOfSessions in a custom base class derived from Controller.

    public void Application_BeginRequest()
    {
        Application.Lock();
        string addr = Request.UserHostAddress;
        Dictionary<string, DateTime> sessions = Application["Sessions"] as Dictionary<string, DateTime>;
        sessions[addr] = DateTime.Now;
        List<string> remove = new List<string>();
        foreach(KeyValuePair<string, DateTime> kvp in sessions)
        {
            TimeSpan span = DateTime.Now - kvp.Value;
            if (span.TotalMinutes > 20)
                remove.Add(kvp.Key);
        }
        foreach (string removeKey in remove)
            sessions.Remove(removeKey);
        BaseController.NumberOfUsers = sessions.Count;
        Application.UnLock();
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜