同一账号禁止多人同时登陆
首先在Global中写如下代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 protected void Session_Start(Object sender, EventArgs e) 2 { 3 ArrayList lstName=new ArrayList(); 4 this.Application.Add("name",lstName); 5 } 6 7 protected void Session_End(Object sender, EventArgs e) 8 { 9 Application.Lock(); 10 string str=Session["name"].ToString(); 11 ArrayList lstName=(ArrayList)this.Application["name"]; 12 Application.UnLock(); 13 }
然后在登陆页面写代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 string username=this.txtName.Text.Trim(); 2 ArrayList lstName=(ArrayList)this.Application["name"]; 3 foreach(string strname in lstName) 4 { 5 if(username.Equals(strname)) 6 { 7 Response.Redirect("User_Login.aspx"); 8 } 9 } 10 11 lstName=(ArrayList)Application["name"]; 12 lstName.Add(this.txtName.Text.Trim()); 13 this.Application.Lock(); 14 this.Application["name"]=lstName; 15 Session["name"]=username; 16 this.Application.UnLock(); 17 this.Response.Redirect("main.aspx");