title: How to prevent multiple user login from the same user id in asp.net using cache?
body: <p>In my asp.net web application i want to prevent multiple user login from the same user name on different machine or the same pc by using Cache.So, in my Global.asax.as page,</p>
  
  <p>i put this line...</p>
  
  <pre><code>protected void Application_PreRequestHandlerExecute(Object sender, EventArgs e)
     {
       if (Session["user"] != null) // e.g. this is after an initial logon
       {
          string sKey = (string)Session["user"];
          string sUser = (string)HttpContext.Current.Cache[sKey];
       }
     }
  </code></pre>
  
  <p>and in my page login submit button click,</p>
  
  <pre><code>string userName=uName.Text;
  string passWord=pwd.Text;
  
  string sKey = uName.Text;
  string sUser = Convert.ToString(Cache[sKey]);
  if (sUser == null || sUser == String.Empty)
             {
                 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"] = TextBox1.Text.Trim();
  
                 if (userName.Equals(db_userName) &amp;&amp; passWord.Equals(db_password))
                 {
                     Response.Write("Welcome " + userName);
                 }
                 else
                 {
                     Response.Write("Invalid Login");
                 }
             }
             else
             {
                 Response.Write("&lt;Marquee&gt;&lt;h1&gt;&lt;font color=red&gt;ILLEGAL LOGIN ATTEMPT!!!&lt;/font&gt;&lt;/h1&gt;&lt;/marquee&gt;");
                 return;
             }
  </code></pre>
  
  <p>But when run my application, It is raising Session state is not available in this context error message on Application_PreRequestHandlerExecute event...</p>
  
  <p>I dont know what is the issue here...So, please guide me to get out of this issue...</p>
  
  <p>Or tell me another good approach instead of this one...</p>