// Cache中没有该str_Key的项目,表名用户没有登录,或者已经登录超时
if (cacheKey == String.Empty)
{
// TimeSpan构造函数,用来重载版本的方法,进行判断是否登录。
TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert(loginUser.UserName, loginUser.UserName, null, DateTime.MaxValue, SessTimeOut, CacheItemPriority.NotRemovable, null);
Session["LoginUser"] = loginUser ;
// 首次登录成功
Response.Write("<h2 style='color:red'>你好,登录成功!");
}
else
{
// 在 Cache 中存在该用户的记录,已经登录过,禁止再次登录
Response.Write("<h2 style='color:red'>抱歉,您好像已经登录了!");
return;
}
}
else
{
Response.Write("用户名称或密码错误!!!");
}
2.注销操作
主页面为非安全登录(如直接关闭浏览器)时,添加js函数处理:
window.onbeforeunload = function() { var n = window.event.screenX - window.screenLeft; var b = n > document.documentElement.scrollWidth-20; //浏览器关闭和刷新都会执行此方法,下面判断是否关闭 if(b && window.event.clientY < 0 || window.event.altKey) //关闭浏览器 { window.location.href="Logout.aspx"; //跳转到安全退出页面
//window.event.returnValue = "警告这是不安全操作!"; //这里可以放置你想做的操作代码 } }
新建layout。aspx页面(主页面安全退出按钮链接此页面),只需在后台添加代码:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["LoginUser"] != null)
{
HttpContext.Current.Cache.Remove((Session["LoginUser"] as UsersDO).UserName);
}
Session.Clear(); Session.Abandon(); FormsAuthentication.SignOut(); Response.Redirect("../index.aspx"); }