title: Forcibly logout all other sessions in case of multiple login by same user
body: <p>I have an application where in im preventing multiple login by same user.If user tries to login from another webpage/machine(when already logged in another) then a confirm box appears asking whether he needs to logout the previous session or not,if he clicks "yes" then the previous sessions shud be logged off.Im not able to logout the previous session however the code is preventing multiple login but how can i logoff the previous session n redirect user to main page with present session</p>
  
  <p>Here is the code<br>
  In  loginButton_Click(after authentication)</p>
  
  <pre><code>    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("&lt;script&gt;if(window.confirm('You have already Logged In.Do you want to sign off the previous Session?')){} else{window.location.href='login.aspx'}&lt;/script&gt;");
          //if part
          return;
      }
  </code></pre>
  
  <p>In Global.asax page</p>
  
  <pre><code>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);
              }
          }
      }
  }
  </code></pre>