在我的前一篇文章《妙用Cache检验用户是否重复登陆》,经过实践和思考,发现忽略了一个很重要的地方:只是在登陆时,设置了一次登录值到Cache中。如果Cache失效的时间设置久了,用户一旦退出,在较短的时间间隔内重新登陆时,会发现无法登陆。但是如果失效时间设置短了,恶意登陆者又会在较短的时间内重新登陆,而且成功通过检验。显然这种判断方法是不完善的。
我们需要怎么来改进这个时间的难题呢?设置一个较短的失效时间间隔,然后每隔一定时间,检查一下Cache,把用户登陆信息重新写入Cache。那么只要用户不退出网站系统,或者不关闭浏览器,这种判断方法将会一直有效!那么,在WEB上,在ASP.NET下,什么东西能方便的实现计时器的效果呢?目前而言,最好的选择无疑是 ATLAS 中的Timer控件!能够设置计时器的启动,间隔时间,以及间隔时间后做的事件。
程序改进以后,分享如下,请参看程序注释:
前台页面
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
%>
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
Untitled Page
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
asp:ScriptManager
ID
="ScriptManager1"
runat
="server"
/>
<
div
>
<
asp:UpdatePanel
ID
="UpdatePanel1"
runat
="server"
>
<
ContentTemplate
>
<
asp:TextBox
ID
="TextBox1"
runat
="server"
></
asp:TextBox
>
<
asp:Button
ID
="Button1"
runat
="server"
OnClick
="Button1_Click"
Text
="登陆"
/>
<
br
/>
<
br
/>
<
asp:Label
ID
="Label1"
runat
="server"
Width
="350px"
></
asp:Label
>
<
asp:Button
ID
="Button2"
runat
="server"
OnClick
="Button2_Click"
Text
="清除Cache"
/>
<
asp:Timer
ID
="Timer1"
runat
="server"
Enabled
="False"
Interval
="15000"
OnTick
="Timer1_Tick"
>
</
asp:Timer
>
</
ContentTemplate
>
</
asp:UpdatePanel
>
</
div
>
</
form
>
</
body
>
</
html
>
后台程序
using
System;
using
System.Data;
using
System.Configuration;
using
System.Web;
using
System.Web.Security;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Web.UI.WebControls.WebParts;
using
System.Web.UI.HtmlControls;
![None.gif](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
partial
class
_Default : System.Web.UI.Page
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected void Page_Load(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
protected void Button1_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//用户名
string sName = TextBox1.Text;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//生成Key
string sKey = sName + "_Login";
//得到Cache中的给定Key的值
string sUser = Convert.ToString(Cache[sKey]);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//检查是否存在
if (sUser == null || sUser == String.Empty)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Session["username"] = sName;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//Cache中没有该Key的项目,表明用户没有登录,或者已经登录超时
//TimeSpan 表示一个时间间隔,获取系统对session超时作的设置值
//(如果考虑到允许用户再次登陆的时间小于session超时时间,可将此值设小)
//TimeSpan SessTimeOut = new TimeSpan(0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);
//这里为了演示,把Cache保存时间间隔设置为了20秒
TimeSpan SessTimeOut = new TimeSpan(0, 0, 0, 20, 0);
HttpContext.Current.Cache.Insert(
sKey,
sKey,
null,
DateTime.MaxValue,
SessTimeOut,
System.Web.Caching.CacheItemPriority.NotRemovable,
null
);
//启动Timer
this.Timer1.Enabled = true;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//首次登录,您可以做您想做的工作了。
Label1.Text = "你好!" + sName + "欢迎光临";
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//在Cache中发现该用户的记录,表示已经登录过,禁止再次登录
Label1.Text = "对不起,你的用户身份已登陆";
return;
}
}
catch (System.Exception ex)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Label1.Text = ex.Message;
}
}
protected void Button2_Click(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//用户名
string sName = TextBox1.Text;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//生成Key
string sKey = sName + "_Login";
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//为了测试方便,设置了这个从Cache中移出登陆信息的方法
HttpContext.Current.Cache.Remove(sKey);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label1.Text = Session["username"] + " 的用户登陆信息已从Cache清除!";
}
protected void Timer1_Tick(object sender, EventArgs e)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (Session["username"] != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//用户名
string sName = TextBox1.Text;
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//生成Key
string sKey = sName + "_Login";
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//得到Cache中的给定Key的值
string sUser = Convert.ToString(Cache[sKey]);
![InBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TimeSpan SessTimeOut = new TimeSpan(0, 0, 0, 20, 0);
if (sUser != null)
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpContext.Current.Cache.Remove(sKey);
}
HttpContext.Current.Cache.Insert(
sKey,
sKey,
null,
DateTime.MaxValue,
SessTimeOut,
System.Web.Caching.CacheItemPriority.NotRemovable,
null
);
}
else
![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Timer1.Enabled = false;
}
}
}
示例代码:/Files/heekui/WebLogin.rar
后记:
1 这个方法对于判断用户重复登陆是可行的,但是同时伴随着另一个问题点。设置了Timer,定时工作的话,只要不是正常退出,或者关闭浏览器的话,Session便永远不会失效了。这样作会有什么不好的效果吗?
2 这个方法对每一个用户而言都会定时向服务器发出请求,无疑会增加服务器端的负担。若同时在线人数很多的情况下,这种请求是否会对服务器产生很大的影响。
所以,只能说以上的这个方法只是一种可行的方法,但是否最优,没有测试。不知各位还有什么更好的办法没有。