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

最近群里和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.  }  
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(); }
然后在登陆页面写代码
  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");  
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");

 

转载于:https://www.cnblogs.com/9who/archive/2008/07/28/1254602.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值