asp.net在线人数限制

1、网站启动初始化在线人数变量

Application["WebsiteCount"] = 0;

2、新的会话进来

  只有在全新的会话进来的时候,该方法才会执行。可以过滤掉某些不需要,限制的页面,比如后台管理系统的页面,在登录后台的时候就不参与限制人数,这样即使进入后台后访问其他页面也不会触发Session_Start方法。

  Session.Abandon();表示删除当前session对象,会触发Session_End方法。
protected void Session_Start(Object sender, EventArgs e)
        {
            Application.Lock();
            Application["WebsiteCount"] = (int)Application["WebsiteCount"] + 1;
            Application.UnLock();
            if (!Request.RawUrl.Equals("/login.aspx",StringComparison.OrdinalIgnoreCase)&&Application["WebsiteCount"] != null && Convert.ToInt32(Application["WebsiteCount"]) > count)
            {
                Session.Abandon();
                Response.Redirect("503.html");

            }

        }

3、会话结束,服务端session过期

 protected void Session_End(Object sender, EventArgs e)
        {
            Application.Lock();
            Application["WebsiteCount"] = (int)Application["WebsiteCount"] - 1;
            Application.UnLock();
        }

 

posted @ 2017-09-16 15:13  随缘梦中人  阅读(649)  评论(0编辑  收藏  举报