利用Cache、Timer(ATLAS)控制用户重复登陆的可行性方法

       在我的前一篇文章《妙用Cache检验用户是否重复登陆》,经过实践和思考,发现忽略了一个很重要的地方:只是在登陆时,设置了一次登录值到Cache中。如果Cache失效的时间设置久了,用户一旦退出,在较短的时间间隔内重新登陆时,会发现无法登陆。但是如果失效时间设置短了,恶意登陆者又会在较短的时间内重新登陆,而且成功通过检验。显然这种判断方法是不完善的。

       我们需要怎么来改进这个时间的难题呢?设置一个较短的失效时间间隔,然后每隔一定时间,检查一下Cache,把用户登陆信息重新写入Cache。那么只要用户不退出网站系统,或者不关闭浏览器,这种判断方法将会一直有效!那么,在WEB上,在ASP.NET下,什么东西能方便的实现计时器的效果呢?目前而言,最好的选择无疑是 ATLAS 中的Timer控件!能够设置计时器的启动,间隔时间,以及间隔时间后做的事件。

 程序改进以后,分享如下,请参看程序注释:
前台页面

ExpandedBlockStart.gif ContractedBlock.gif <% dot.gif @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"  %>
None.gif
None.gif
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" >
None.gif
< html  xmlns ="http://www.w3.org/1999/xhtml" >
None.gif
< head  runat ="server" >
None.gif    
< title > Untitled Page </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form  id ="form1"  runat ="server" >
None.gif        
< asp:ScriptManager  ID ="ScriptManager1"  runat ="server"   />
None.gif        
< div >
None.gif            
< asp:UpdatePanel  ID ="UpdatePanel1"  runat ="server" >
None.gif                
< ContentTemplate >
None.gif                    
< asp:TextBox  ID ="TextBox1"  runat ="server" ></ asp:TextBox >
None.gif                    
< asp:Button  ID ="Button1"  runat ="server"  OnClick ="Button1_Click"  Text ="登陆"   />
None.gif                    
< br  />
None.gif                    
< br  />
None.gif                    
< asp:Label  ID ="Label1"  runat ="server"  Width ="350px" ></ asp:Label >
None.gif                    
< asp:Button  ID ="Button2"  runat ="server"  OnClick ="Button2_Click"  Text ="清除Cache"   /> &nbsp;
None.gif                    
< asp:Timer  ID ="Timer1"  runat ="server"  Enabled ="False"  Interval ="15000"  OnTick ="Timer1_Tick" >
None.gif                    
</ asp:Timer >
None.gif                
</ ContentTemplate >
None.gif            
</ asp:UpdatePanel >
None.gif        
</ div >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >
None.gif

后台程序

None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
public  partial  class  _Default : System.Web.UI.Page 
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//用户名
InBlock.gif
            string sName = TextBox1.Text;
InBlock.gif
InBlock.gif            
//生成Key   
InBlock.gif
            string sKey = sName + "_Login";
InBlock.gif            
InBlock.gif            
//得到Cache中的给定Key的值   
InBlock.gif
            string sUser = Convert.ToString(Cache[sKey]);
InBlock.gif
InBlock.gif            
//检查是否存在   
InBlock.gif
            if (sUser == null || sUser == String.Empty)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Session[
"username"= sName;
InBlock.gif
InBlock.gif                
//Cache中没有该Key的项目,表明用户没有登录,或者已经登录超时      
InBlock.gif                
//TimeSpan 表示一个时间间隔,获取系统对session超时作的设置值
InBlock.gif                
//(如果考虑到允许用户再次登陆的时间小于session超时时间,可将此值设小)  
InBlock.gif                
//TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
InBlock.gif                
//这里为了演示,把Cache保存时间间隔设置为了20秒
InBlock.gif
                TimeSpan SessTimeOut = new TimeSpan(000200);
InBlock.gif                HttpContext.Current.Cache.Insert(
InBlock.gif                                                    sKey, 
InBlock.gif                                                    sKey, 
InBlock.gif                                                    
null
InBlock.gif                                                    DateTime.MaxValue, 
InBlock.gif                                                    SessTimeOut,
InBlock.gif                                                    System.Web.Caching.CacheItemPriority.NotRemovable, 
InBlock.gif                                                    
null
InBlock.gif                                                 );
InBlock.gif                
InBlock.gif                
//启动Timer
InBlock.gif
                this.Timer1.Enabled = true;
InBlock.gif
InBlock.gif                
//首次登录,您可以做您想做的工作了。   
InBlock.gif
                Label1.Text = "你好!" + sName + "欢迎光临";
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//在Cache中发现该用户的记录,表示已经登录过,禁止再次登录   
InBlock.gif
                Label1.Text = "对不起,你的用户身份已登陆";
InBlock.gif                
return;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
catch (System.Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Label1.Text 
= ex.Message;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Button2_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//用户名
InBlock.gif
        string sName = TextBox1.Text;
InBlock.gif
InBlock.gif        
//生成Key   
InBlock.gif
        string sKey = sName + "_Login";
InBlock.gif
InBlock.gif        
//为了测试方便,设置了这个从Cache中移出登陆信息的方法
InBlock.gif
        HttpContext.Current.Cache.Remove(sKey);
InBlock.gif
InBlock.gif        Label1.Text 
= Session["username"+ " 的用户登陆信息已从Cache清除!";
ExpandedSubBlockEnd.gif    }

InBlock.gif    
protected void Timer1_Tick(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
if (Session["username"!= null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//用户名
InBlock.gif
            string sName = TextBox1.Text;
InBlock.gif
InBlock.gif            
//生成Key   
InBlock.gif
            string sKey = sName + "_Login";
InBlock.gif
InBlock.gif            
//得到Cache中的给定Key的值   
InBlock.gif
            string sUser = Convert.ToString(Cache[sKey]);
InBlock.gif
InBlock.gif            TimeSpan SessTimeOut 
= new TimeSpan(000200);
InBlock.gif            
if (sUser != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                HttpContext.Current.Cache.Remove(sKey);
ExpandedSubBlockEnd.gif            }

InBlock.gif            HttpContext.Current.Cache.Insert(
InBlock.gif                                                sKey, 
InBlock.gif                                                sKey, 
InBlock.gif                                                
null
InBlock.gif                                                DateTime.MaxValue, 
InBlock.gif                                                SessTimeOut,
InBlock.gif                                                System.Web.Caching.CacheItemPriority.NotRemovable, 
InBlock.gif                                                
null
InBlock.gif                                             );
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Timer1.Enabled = false;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

示例代码:/Files/heekui/WebLogin.rar

后记:
1  这个方法对于判断用户重复登陆是可行的,但是同时伴随着另一个问题点。设置了Timer,定时工作的话,只要不是正常退出,或者关闭浏览器的话,Session便永远不会失效了。这样作会有什么不好的效果吗?
2  这个方法对每一个用户而言都会定时向服务器发出请求,无疑会增加服务器端的负担。若同时在线人数很多的情况下,这种请求是否会对服务器产生很大的影响。
    所以,只能说以上的这个方法只是一种可行的方法,但是否最优,没有测试。不知各位还有什么更好的办法没有。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值