title: control user's online status in asp.net
body: <p>I my application I do not want the same user name login at the same time,so I have and idea but I am not sure if it is correct.</p>
  
  <p>1) When a user login,update the status(the "isOnLine" column in the user table in db) and save its login time in the session ,something like:</p>
  
  <p>Inside the login method:</p>
  
  <pre><code>DateTime ltime=Datetime.now();
  
  Dbservice.executeSql(update User set(isOnLine,lastLoginTime) value("1",ltime));
  
  Session["logintime"]=ltime;
  </code></pre>
  
  <p>When another user try to login,check the table to see if the status of this user is logined or not.if yes,set the "isOnline" to "0",then he can login now.</p>
  
  <p>2)In each protected page's Page_Onload() method,check if the login time in the session is equal to the time in db:</p>
  
  <pre><code>string logtime=Dbservice.executeSelect("select lastLoginTime from user where xxxxx").Rows[0]["lastLoginTime"];
  
  if(!Session["logintime"]==logtime){
  
    //this user should offline now,redirect it to the login page
  
  }
  </code></pre>
  
  <p>I wonder if my way is right or not?</p>
  
  <p>Also,I have to write the check logic in each protected page's Page_onLoad method,so there are so many repeat codes,any ideas to avoid this?</p>
  
  <p>Since all the page in our site is protected!.</p>
  
  <p>Thanks.</p>
  
  <p>UPDATE:</p>
  
  <p>It is not allowed two user online at the same time,but it is allowed the later user with the correct pasword can force the former user offline. For example:</p>
  
  <blockquote>
    <p>user1 login with "username=bill" and
    "password=000",then he is online now.</p>
    
    <p>then user2 try to login with
    "username=bill" and
    "password=123",since his password is
    not valid,so his request is denied.</p>
    
    <p>user3 try to login with
    "username=bill" and
    "password=000",since his password is
    valid,so he have the choice to make
    the user1 offline.</p>
  </blockquote>
  
  <p>In this case,when the later user login sucessfully,but the session of the former user is also exist,so I have to check if it is online or not according the "logintime" in the session.</p>