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

2008-02-22 09:41:54来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折


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

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:asp.net定制简单的错误处理页面

下一篇:"ASP.NET AJAX RC1(疑似缺陷)"及"URL重写"及"浏览器缓存"三个凑