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
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




