asp.net用户登录并发数控制

来源:互联网 发布:好吃的甜品店淘宝 编辑:程序博客网 时间:2024/05/28 20:18

              登录成功后,得到允许该用户并发数的数量onlineNum;name为该用户的用户名;

               Application[name + "_count"]=onlieNum;

 

               if (Application[name + "_login"] == null)
                {
                    string id = SiteSession.SessionId;
                    ArrayList arr = new ArrayList();
                    arr.Add(id);
                    Application[name + "_login"] = arr;
                }
                else
                {
                    ArrayList arr = new ArrayList();
                    arr = (ArrayList)Application[name + "_login"];
                    if (arr.Count < Convert.ToInt16(Application[name + "_count"]))
                    {
                        string id = SiteSession.SessionId;
                        arr.Add(id);
                        Application[name + "_login"] = arr;

                    }
                    else
                    {
                        MessageBox.Show(Page, "超过最大连接数,请稍后再试");
                        return;
                    }
                }

 

 

 

在global.asax文件中(防止意外退出)

 void Session_End(object sender, EventArgs e)
    {
        // 在会话结束时运行的代码。
        // 注意: 只有在 Web.config 文件中的 sessionstate 模式设置为

        // InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer
        // 或 SQLServer,则不会引发该事件。
  
        ArrayList arr = (ArrayList)Application[HttpContext.Current.Session["UserAccount"].ToString()+"_login"];
        if(arr.Contains(HttpContext.Current.Session.SessionID))
        {
            arr.Remove(HttpContext.Current.Session.SessionID);
           
        }  
          
       


    }