我有一个应用程序,我阻止同一用户多次登录。如果用户尝试从另一个网页/机器登录(当已经登录另一个),则会出现一个确认框,询问他是否需要注销上一个会话,如果他单击“是”,然后以前的会话将被注销。我无法注销上一个会话,但是代码阻止了多次登录,但是我如何注销上一个会话 n 将用户重定向到当前会话的主页
这是
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);
}
}
}
}