asp.Netワンポイントログイン、ブラウザクローズクリアセッション
4864 ワード
1.ログインの検証:
UsersDO usersDO = new UsersDO();
usersDO.UserName = this.txtUserName.Text.Trim();
usersDO.Pwd = Tools.EncryptString(this.txtPwd.Text);
UsersDO loginUser= new UsersDAO().GetUsersByLogin(usersDO);
if (loginUser != null)
{
string cacheKey = Convert.ToString(Cache[loginUser.UserName]);
HttpContext.Current.Cache.Remove((Session["LoginUser"] as UsersDO).UserName);
}
Session.Clear(); Session.Abandon(); FormsAuthentication.SignOut(); Response.Redirect("../index.aspx"); }
UsersDO usersDO = new UsersDO();
usersDO.UserName = this.txtUserName.Text.Trim();
usersDO.Pwd = Tools.EncryptString(this.txtPwd.Text);
UsersDO loginUser= new UsersDAO().GetUsersByLogin(usersDO);
if (loginUser != null)
{
string cacheKey = Convert.ToString(Cache[loginUser.UserName]);
// 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"); }