string sKey = username.Text.ToString().Trim(); //
得到Cache中的给定Key的值
string sUser = Convert.ToString(Cache[sKey]); // 检查是否存在
if (sUser
== null || sUser ==
String.Empty)
{
TimeSpan SessTimeOut = new TimeSpan(0, 0,
System.Web.HttpContext.Current.Session.Timeout, 0,
0);//取得Session的过期时间
HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue,
SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable,
null);//将值放入cache己方便单点登录
//成功登录
}
else if
(Cache[sKey].ToString() ==
sKey)//如果这个账号已经登录
{
ClientScript.RegisterStartupScript(GetType(), "提示",
"<script>alert(‘对不起,当前用户已经登录‘);</script>");
return;
}
else
{
Session.Abandon();//这段主要是为了避免不必要的错误导致不能登录
}
原文地址:http://www.cnblogs.com/kaiwanlin/p/3714302.html