title: How to prevent multiple login from single user name
body: <p>How to prevent multiple login from single user name? I am saving username and password in database. I want the user to login from 1 place at a time only.</p>
  
  <p>For more information on how to configure your ASP.NET application, please visit <a href="http://go.microsoft.com/fwlink/?LinkId=169433" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkId=169433</a></p>
  
  <p>Below is a code which I have tried</p>
  
  <p>Config File                             </p>
  
  <pre><code>&lt;configuration&gt;                             
      &lt;system.web&gt;
          &lt;compilation debug="true" targetFramework="4.5" /&gt;
          &lt;httpRuntime targetFramework="4.5" /&gt;
          &lt;httpModules&gt;
              &lt;add name="SingleSessionEnforcement" type="SingleSessionEnforcement" /&gt;
          &lt;/httpModules&gt;
      &lt;/system.web&gt;
      &lt;system.webServer&gt;
          &lt;validation validateIntegratedModeConfiguration="false"/&gt;
          &lt;modules  runAllManagedModulesForAllRequests="false" &gt;
              &lt;add name="SingleSessionEnforcement" type="SingleSessionEnforcement" /&gt;
          &lt;/modules&gt;
      &lt;/system.webServer&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>Class File</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.Security;
  
  namespace Demo1
  {
      public class SingleSessionEnforcement : IHttpModule
      {
          public SingleSessionEnforcement()
          {
              // No construction needed
          }
  
          private void OnPostAuthenticate(Object sender, EventArgs e)
          {
              Guid sessionToken;
  
              HttpApplication httpApplication = (HttpApplication)sender;
              HttpContext httpContext = httpApplication.Context;
  
              // Check user's session token
              if (httpContext.User.Identity.IsAuthenticated)
              {
                  FormsAuthenticationTicket authenticationTicket =
                                              ((FormsIdentity)httpContext.User.Identity).Ticket;
  
                  if (authenticationTicket.UserData != "")
                  {
                      sessionToken = new Guid(authenticationTicket.UserData);
                  }
                  else
                  {
                      // No authentication ticket found so logout this user
                      // Should never hit this code
                      FormsAuthentication.SignOut();
                      FormsAuthentication.RedirectToLoginPage();
                      return;
                  }
  
                  MembershipUser currentUser = Membership.GetUser(authenticationTicket.Name);
  
                  // May want to add a conditional here so we only check
                  // if the user needs to be checked. For instance, your business
                  // rules for the application may state that users in the Admin
                  // role are allowed to have multiple sessions
                  Guid storedToken = new Guid(currentUser.Comment);
  
                  if (sessionToken != storedToken)
                  {
                      // Stored session does not match one in authentication
                      // ticket so logout the user
                      FormsAuthentication.SignOut();
                      FormsAuthentication.RedirectToLoginPage();
                  }
              }
          }
  
          public void Dispose()
          {
              // Nothing to dispose
          }
  
          public void Init(HttpApplication context)
          {
              context.PostAuthenticateRequest += new EventHandler(OnPostAuthenticate);
          }
      }
  }
  </code></pre>
  
  <p>ASPX.cs</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  
  namespace Demo1
  {
      public partial class login : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
          }
  
          protected void Button1_Click(object sender, EventArgs e)
          {
              //TextBox userNameTextBox = (TextBox)LoginUser.FindControl("UserName");
              SingleSessionPreparation.CreateAndStoreSessionToken(userNameTextBox.Text);
          }
  
      }
  }
  </code></pre>
  
  <p>ASPX</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="Demo1.login" %&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form id="form1" runat="server"&gt;
          &lt;div&gt;
              Name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;    &lt;asp:TextBox ID="userNameTextBox" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;br/&gt;
              Password:    &lt;asp:TextBox ID="PasswordTextBox" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;br/&gt;
                      &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
              &lt;asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>It is giving this error:</p>
  
  <blockquote>
    <p>Could not load type 'SingleSessionEnforcement'.<br>
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.    </p>
    
    <p>Exception Details: System.Web.HttpException: Could not load type 'SingleSessionEnforcement'.</p>
  </blockquote>
  
  <p>Please refere the attached image of the error: </p>
  
  <p><img src="https://i.stack.imgur.com/yIVVb.png" alt="Error image"></p>
  
  <p>Please guide me to solve the issue.</p>