我在哪裏即時防止多個登錄由相同user.If用戶試圖從其他網頁/機登錄(在另一個已經登錄時)的應用程序然後將確認框,詢問他是否需要註銷前一交易日與否,如果他點擊「是」,那麼以前的會議shud登錄off.Im無法註銷前一交易日然而代碼是防止多次登錄,但我怎麼能註銷時先前的會話重定向ñ用戶主頁與存在會話強制註銷中多個登錄的情況下,所有其它會話由相同的用戶
下面是代碼
在loginButton_Click(認證後)
string sKey = loginControl.UserName + loginControl.Password;
string sUser = Convert.ToString(Cache[sKey]);
if (sUser == null || sUser == String.Empty || sUser == "")
{
TimeSpan SessTimeOut = new TimeSpan(0, 0, HttpContext.Current.Session.Timeout, 0, 0);
HttpContext.Current.Cache.Insert(sKey, sKey, null, DateTime.MaxValue, SessTimeOut, System.Web.Caching.CacheItemPriority.NotRemovable, null);
Session["user"] = loginControl.UserName + loginControl.Password;
Response.Redirect("MainPage.aspx");
}
else
{
Response.Write("<script>if(window.confirm('You have already Logged In.Do you want to sign off the previous Session?')){} else{window.location.href='login.aspx'}</script>");
//if part
return;
}
在Global.asax中頁面
protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
{
if (System.Web.HttpContext.Current.Session != null)
{
if (Session["user"] != null)
{
string sKey = (string)Session["user"];
string sUser = (string)HttpContext.Current.Cache[sKey];
}
else
{
foreach (DictionaryEntry de in HttpContext.Current.Cache)
{
HttpContext.Current.Cache.Remove((string)de.Key);
}
}
}
}
請使用您的IDE和編輯器中的代碼按鈕[格式化您的代碼塊](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks )正確。 –