.NET同一账号禁止多人同时登陆
最近群里和csdn上都有人提问关于 同一账号禁止多人同时登陆的问题,今天写了一个与大家分享下.
首先在Global中写如下代码:
- protected void Session_Start(Object sender, EventArgs e)
- ...{
- ArrayList lstName=new ArrayList();
- this.Application.Add("name",lstName);
- }
- //****************************************************************************************************8
- protected void Session_End(Object sender, EventArgs e)
- ...{
- Application.Lock();
- string str=Session["name"].ToString();
- ArrayList lstName=(ArrayList)this.Application["name"];
- Application.UnLock();
- }
然后在登陆页面写代码
- string username=this.txtName.Text.Trim();
- ArrayList lstName=(ArrayList)this.Application["name"];
- foreach(string strname in lstName)
- ...{
- if(username.Equals(strname))
- ...{
- Response.Redirect("User_Login.aspx");
- }
- }
- lstName=(ArrayList)Application["name"];
- lstName.Add(this.txtName.Text.Trim());
- this.Application.Lock();
- this.Application["name"]=lstName;
- Session["name"]=username;
- this.Application.UnLock();
- this.Response.Redirect("main.aspx");