同一账号禁止多人同时登陆

来源:互联网 发布:经典爱情电影知乎 编辑:程序博客网 时间:2024/05/01 21:01

最近群里和csdn上都有人提问关于 同一账号禁止多人同时登陆的问题,今天写了一个与大家分享下.

首先在Global中写如下代码:

  1. protected void Session_Start(Object sender, EventArgs e)   
  2.  ...{   
  3.  ArrayList lstName=new ArrayList();   
  4.  this.Application.Add("name",lstName);   
  5.  }   
  6.     
  7.  //****************************************************************************************************8   
  8.  protected void Session_End(Object sender, EventArgs e)   
  9.  ...{   
  10.  Application.Lock();   
  11.  string str=Session["name"].ToString();   
  12.  ArrayList lstName=(ArrayList)this.Application["name"];   
  13.  Application.UnLock();   
  14.  }  

然后在登陆页面写代码
  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");