Asp.net中实现同一用户名不能同时登录(单点登录)

最近找了一些单点登录的,发现了这篇文章,貌似还是可以实现的,先保存了。

Web 项目中经常遇到的问题就是同一用户名多次登陆的问题,相应的解决办法也很多,总结起来不外乎这几种解决办法:将登陆后的用户名放到数据库表中;登陆后的用户名放到Session中;登陆后的用户名放到Application中;登陆后的用户名放到Cache中。一般的这几种方法都是登陆了之后,如果没有正常退出,第二次登陆将不被允许。这样一般都会存在一个问题:如果用户没有正常退出系统,那么他接下来继续登陆的时候,因为Session没有过期等问题,会被拒绝继续登陆系统,只能等待Session过期后才能登陆。本文介绍的方法是采用类似于MSN登陆的方法,第二次登陆时会把第一次的登陆注销掉,第一次登陆将会类似于MSN弹出:您的帐号已在别处被登陆,您被强迫下线的提示信息。<wbr><br><span style="line-height:1.3em">功能实现起来也比较简单:</span><wbr><br><span style="line-height:1.3em">登陆用户名密码验证通过之后输入以下代码:</span><wbr><br><span style="line-height:1.3em">Hashtable hOnline = (Hashtable)Application["Online"];</span><wbr><br><span style="line-height:1.3em">if(hOnline != null)</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">IDictionaryEnumerator idE = hOnline.GetEnumerator();</span><wbr><br><span style="line-height:1.3em">string strKey = "";</span><wbr><br><span style="line-height:1.3em">while(idE.MoveNext())</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">if(idE.Value != null &amp;&amp; idE.Value.ToString().Equals(UserID))</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">//already login </span><wbr><br><span style="line-height:1.3em">strKey = idE.Key.ToString();</span><wbr><br><span style="line-height:1.3em">hOnline[strKey] = "XXXXXX";</span><wbr><br><span style="line-height:1.3em">break;</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">else</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">hOnline = new Hashtable();</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">hOnline[Session.SessionID] = UserID;</span><wbr><br><span style="line-height:1.3em">Application.Lock();</span><wbr><br><span style="line-height:1.3em">Application["Online"] = hOnline;</span><wbr><br><span style="line-height:1.3em">Application.UnLock();</span><wbr><br><span style="line-height:1.3em">用户登陆的时候将登陆用户名放在一个全局变量Online,Online为Hashtable结构,Key为SessionID,Value为用户名。每次用户登陆时均判断以下要登陆的用户名在Online中是不是已经存在,如果存在该用户名已经被登陆,将第一个人登陆的SessionID对应的用户名强制变更为XXXXXX,表示该登陆将被强制注销。</span><wbr><br><span style="line-height:1.3em">建立一个CommonPage页,系统中所有的页面都继承于CommonPage页,在CommonPage页的后台代码中添加如下代码:</span><wbr><br><span style="line-height:1.3em">override protected void OnInit(EventArgs e)</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">Hashtable hOnline = (Hashtable)Application["Online"];</span><wbr><br><span style="line-height:1.3em">if(hOnline != null)</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">IDictionaryEnumerator idE = hOnline.GetEnumerator();</span><wbr><br><span style="line-height:1.3em">while(idE.MoveNext())</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">if(idE.Key != null &amp;&amp; idE.Key.ToString().Equals(Session.SessionID))</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">//already login</span><wbr><br><span style="line-height:1.3em">if(idE.Value != null &amp;&amp; "XXXXXX".Equals(idE.Value.ToString()))</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">hOnline.Remove(Session.SessionID);</span><wbr><br><span style="line-height:1.3em">Application.Lock();</span><wbr><br><span style="line-height:1.3em">Application["Online"] = hOnline;</span><wbr><br><span style="line-height:1.3em">Application.UnLock();</span><wbr><br><span style="line-height:1.3em">MessageBox("你的帐号已在别处登陆,你被强迫下线!",Login.aspx);</span><wbr><br><span style="line-height:1.3em">return false;</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">break;</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">继承于CommonPage的页面在刷新时都要执行重载的OnInit中的代码,取出Online,找到该用户对应的SessionID,判断SessionID里对应的用户名是否变更,如果变更,就强迫下线,清掉Session,转到Login画面。</span><wbr><br><span style="line-height:1.3em">最后需要在Session过期或者退出系统时释放资源,在Global.asax文件中的Session_End中添加如下代码:</span><wbr><br><span style="line-height:1.3em">Hashtable hOnline = (Hashtable)Application["Online"];</span><wbr><br><span style="line-height:1.3em">if(hOnline[Session.SessionID] != null)</span><wbr><br><span style="line-height:1.3em">{</span><wbr><br><span style="line-height:1.3em">hOnline.Remove(Session.SessionID);</span><wbr><br><span style="line-height:1.3em">Application.Lock();</span><wbr><br><span style="line-height:1.3em">Application["Online"] = hOnline;</span><wbr><br><span style="line-height:1.3em">Application.UnLock();</span><wbr><br><span style="line-height:1.3em">}</span><wbr><br><span style="line-height:1.3em">如果用户不正常退出后重登陆,因为重登陆的优先级大,不会影响用户的登陆,而不正常退出的用户占用的资源会在Session过期后自动清除,不会影响系统的性能。</span><br><br><p id="TBPingURL">Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=2095105<br><br></p> <div class="tit">关于WEB系统单点登陆问题<br> </div> <div class="date"> <span style="font-family:Verdana"><a href="http://hi.baidu.com/fnhamwsc/blog/item/c70afc948c15581ad31b7074.html" target="_blank">http://hi.baidu.com/fnhamwsc/blog/item/c70afc948c15581ad31b7074.html</a></span> 2008年01月04日 星期五 08:19</div> <div class="cnt">为保证WEB系统安全,需要具有单点登陆检测功能,Google了一下下,发现不少方法,最后根据<a href="http://www.cnblogs.com/wliang22/admin/file::;" target="_blank"><span style="color:#ff6600; line-height:1.3em">水月神坛 zyc21st的专栏</span><wbr></wbr></a><wbr><span style="color:#ff6600; line-height:1.3em">上</span><wbr>的一篇文章《<span style="color:#000000; line-height:1.3em">Asp.net中实现同一用户名不能同时登陆(单点登陆)</span><wbr> 》实现,但使用过程中有个小BUG,所以做了小小修改。 <br><span style="color:#ff0000; line-height:1.3em">1)</span><wbr>密码验证后: <br> Hashtable hOnline = (Hashtable)Application["Online"]; <br> if (hOnline != null) <br> { <br><span style="color:#ff6600; line-height:1.3em">int i = 0; </span> <wbr><br><span style="color:#ff6600; line-height:1.3em"> while (i&lt;hOnline.Count) //因小BUG所以增加此判断,强制查询到底</span><wbr><br><span style="color:#ff6600; line-height:1.3em"> { </span> <wbr><br> IDictionaryEnumerator idE = hOnline.GetEnumerator(); <br> string strKey = ""; <br> while (idE.MoveNext()) <br> { <br> if (idE.Value != null &amp;&amp; idE.Value.ToString().Equals(this.username.Text))<br> { <br> //already login <br> strKey = idE.Key.ToString(); <br> hOnline[strKey] = "XXXXXX"; <br> break; <br> } <br> } <br><span style="color:#ff6600; line-height:1.3em"> i = i + 1;</span><wbr><br><span style="color:#ff6600; line-height:1.3em"> } </span> <wbr><br> } <br> else <br> { <br> hOnline = new Hashtable(); <br> } <br> hOnline[Session.SessionID] = this.username.Text; <br> Application.Lock(); <br> Application["Online"] = hOnline; <br> Application.UnLock(); <br> //用户登陆的时候将登陆用户名放在一个全局变量Online,Online为Hashtable结构, <br> //Key为SessionID,Value为用户名。每次用户登陆时均判断以下要登陆的用户名在Online中是不是已经存在,<br> //如果存在该用户名已经被登陆,将第一个人登陆的SessionID对应的用户名强制变更为XXXXXX,表示该登陆将被强制注销<br><br><br><span style="color:#ff0000; line-height:1.3em">2)</span><wbr>建立一个CommonPage页,系统中所有的页面都继承于CommonPage页(<span style="color:#ff9900; line-height:1.3em">public partial class index : CommonPage</span><wbr>),在CommonPage页的后台代码中添加如下代码:<br> using System; <br> using System.Data; <br> using System.Configuration; <br> using System.Web; <br> using System.Web.Security; <br> using System.Web.UI; <br> using System.Web.UI.WebControls; <br> using System.Web.UI.WebControls.WebParts; <br> using System.Web.UI.HtmlControls; <br><span style="color:#ff6600; line-height:1.3em">using System.Collections;</span><wbr><br> /// &lt;summary&gt; <br> /// CommonPage 防止用户多点登陆 <br> /// &lt;/summary&gt; <br> public class CommonPage: System.Web.UI.Page <br> { <br><br> public CommonPage() <br> { <br> // <br> // TODO: 在此处添加构造函数逻辑 <br> // <br> } <br> override protected void OnInit(EventArgs e) <br> { <br> Hashtable hOnline = (Hashtable)Application["Online"]; <br> if (hOnline != null) <br> { <br> IDictionaryEnumerator idE = hOnline.GetEnumerator(); <br> while (idE.MoveNext()) <br> { <br> if (idE.Key != null &amp;&amp; idE.Key.ToString().Equals(Session.SessionID))<br> { <br> //already login <br> if (idE.Value != null &amp;&amp; "XXXXXX".Equals(idE.Value.ToString()))<br> { <br> hOnline.Remove(Session.SessionID); <br> Application.Lock(); <br> Application["Online"] = hOnline; <br> Application.UnLock(); <br><span style="color:#ff6600; line-height:1.3em">string js = "&lt;script language=javascript&gt;alert('{0}');window.location.replace('{1}')&lt;/script&gt;";</span><wbr><br><span style="color:#ff6600; line-height:1.3em"> Response.Write(string.Format(js, "帐号已在别处登陆,你将被强迫下线(请保管好自己的用户密码)!", "logout.aspx?cname=noadmin"));</span><wbr><br><br><span style="color:#ff6600; line-height:1.3em"> return; </span> <wbr><br> } <br> break; <br> } <br> } <br> } <br> } <br><br> } <br><br> 继承于CommonPage的页面在刷新时都要执行重载的OnInit中的代码,取出Online,找到该用户对应的SessionID,判断SessionID里对应的用户名是否变更,如果变更,就强迫下线,清掉Session,转到Login画面。<br><span style="color:#ff0000; line-height:1.3em">3)</span><wbr>最后需要在Session过期或者退出系统时释放资源,在Global.asax文件中的Session_End中添加如下代码:<br> Hashtable hOnline = (Hashtable)Application["Online"]; <br> if(hOnline[Session.SessionID] != null) <br> { <br> hOnline.Remove(Session.SessionID); <br> Application.Lock(); <br> Application["Online"] = hOnline; <br> Application.UnLock(); <br> } <br> 如果用户不正常退出后重登陆,因为重登陆的优先级大,不会影响用户的登陆,而不正常退出的用户占用的资源会在Session过期后自动清除,不会影响系统的性能。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr> </div> </wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值