利用cache、timer(atlas)控制用户重复登陆的可行性方法
在我的前一篇文章《妙用cache检验用户是否重复登陆》,经过实践和思考,发现忽略了一个很重要的地方:只是在登陆时,设置了一次登录值到cache中。如果cache失效的时间设置久了,用户一旦退出,在较短的时间间隔内重新登陆时,会发现无法登陆。但是如果失效时间设置短了,恶意登陆者又会在较短的时间内重新登陆,而且成功通过检验。显然这种判断方法是不完善的。
我们需要怎么来改进这个时间的难题呢?设置一个较短的失效时间间隔,然后每隔一定时间,检查一下cache,把用户登陆信息重新写入cache。那么只要用户不退出网站系统,或者不关闭浏览器,这种判断方法将会一直有效!那么,在web上,在asp.net下,什么东西能方便的实现计时器的效果呢?目前而言,最好的选择无疑是 atlas 中的timer控件!能够设置计时器的启动,间隔时间,以及间隔时间后做的事件。
程序改进以后,分享如下,请参看程序注释:
前台页面
<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %>
<!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;
public partial class _default : system.web.ui.page
{
protected void page_load(object sender, eventargs e)
{
}
protected void button1_click(object sender, eventargs e)
{
try
{
//用户名
string sname = textbox1.text;
//生成key
string skey = sname + "_login";
//得到cache中的给定key的值
string suser = convert.tostring(cache[skey]);
//检查是否存在
if (suser == null || suser == string.empty)
{
session["username"] = sname;
//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;
//首次登录,您可以做您想做的工作了。
label1.text = "你好!" + sname + "欢迎光临";
}
else
{
//在cache中发现该用户的记录,表示已经登录过,禁止再次登录
label1.text = "对不起,你的用户身份已登陆";
return;
}
}
catch (system.exception ex)
{
label1.text = ex.message;
}
}
protected void button2_click(object sender, eventargs e)
{
//用户名
string sname = textbox1.text;
//生成key
string skey = sname + "_login";
//为了测试方便,设置了这个从cache中移出登陆信息的方法
httpcontext.current.cache.remove(skey);
label1.text = session["username"] + " 的用户登陆信息已从cache清除!";
}
protected void timer1_tick(object sender, eventargs e)
{
if (session["username"] != null)
{
//用户名
string sname = textbox1.text;
//生成key
string skey = sname + "_login";
//得到cache中的给定key的值
string suser = convert.tostring(cache[skey]);
timespan sesstimeout = new timespan(0, 0, 0, 20, 0);
if (suser != null)
{
httpcontext.current.cache.remove(skey);
}
httpcontext.current.cache.insert(
skey,
skey,
null,
datetime.maxvalue,
sesstimeout,
system.web.caching.cacheitempriority.notremovable,
null
);
}
else
{
this.timer1.enabled = false;
}
}
}
示例代码:/files/heekui/weblogin.rar
后记:
1 这个方法对于判断用户重复登陆是可行的,但是同时伴随着另一个问题点。设置了timer,定时工作的话,只要不是正常退出,或者关闭浏览器的话,session便永远不会失效了。这样作会有什么不好的效果吗?
2 这个方法对每一个用户而言都会定时向服务器发出请求,无疑会增加服务器端的负担。若同时在线人数很多的情况下,这种请求是否会对服务器产生很大的影响。
所以,只能说以上的这个方法只是一种可行的方法,但是否最优,没有测试。不知各位还有什么更好的办法没有。
http://www.cnblogs.com/heekui/archive/2007/01/08/615254.html