.net教程:妙用cache检验用户是否重复登陆
网上看到可用cache来判断用户是否已登陆的方法,感觉还不错。实验后,特此分享代码
原理比较简单:
判断cache中是否已存在规定的客户登陆字符串,如果没有便添加,同时指定其在cache中的保存时间。重复登陆时,便能通过判断cache值是否为空来判断用户是否重复登陆了。
//生成key
string skey = textbox1.text + "_islogin";
//得到cache中的给定key的值
string suser = convert.tostring(cache[skey]);
//检查是否存在
if (suser == null || suser == string.empty)
{
//cache中没有该key的项目,表明用户没有登录,或者已经登录超时
//timespan 表示一个时间间隔,获取系统对session超时作的设置值
//timespan sesstimeout = new timespan(0, 0, system.web.httpcontext.current.session.timeout, 0, 0);
//(如果考虑到允许用户再次登陆的时间小于session超时时间,可将此值设小,在此示例中设置为一分钟)
timespan sesstimeout = new timespan(0, 0, 1, 0, 0);
httpcontext.current.cache.insert(skey, skey, null, datetime.maxvalue, sesstimeout,
system.web.caching.cacheitempriority.notremovable, null);
//首次登录,您可以做您想做的工作了。
label1.text = "你好!欢迎光临";
}
else
{
//在cache中发现该用户的记录,表名已经登录过,禁止再次登录
label1.text = "对不起,你已在别处登陆.或者在1分钟后重试";
return;
}
http://www.cnblogs.com/heekui/archive/2006/12/14/591691.html