Preventing Simultaneous Logons - The Traditional Approach
(Page 2 of 4 )
Let us create a Boolean datatype in our database, as part of the User table. I have named it isLogged for the sake of clarity. As soon as the login process, for a given user account, succeeds, the value of this flag would be set to True. Now, let me take a minute to define the success of the login process.
During the login process, as part of verifying the credentials against the User table, we also check for the value of isLogged. If the value is already set to True, it clearly means that the user is already logged on to the system. Aha! Gotcha! We redirect the user to an appropriate page clearly explaining the problem and thus login fails. On the other hand, if the value of isLogged is False, we set it True and allow the user to proceed further with the application.
One of the easier ways to implement this logic is to make use of the global.asax. This file has two routines namely Session_onStart and Session_onEnd. I am sure the names are self-explanatory. So we put the code login process in Session_onStart and set the value of isLogged to False in Session_onEnd. This would be our simple and straightforward logout process.
The Flip Side to It
Ok, now what? The traditional approach would work perfectly under normal scenarios (i.e. a user logs onto our site, has some good time minding their own business and logs out). What will happen if he/she doesn’t log out and just closes the browser; or what if he simply moves on to visit another site without logging out?
One of obvious reasons we can’t trust Session_onEnd is that there is no guarantee that the event will be triggered when the browser is closed. Does this mean that the entire purpose of Session_onEnd is defeated? Maybe not. The event will be triggered after the session times out. Normally, this time would be set to about 20 minutes. So there are chances that our user gets frustrated and leaves our website once and for all.
Next: Data Caching to Our Rescue >>
More ASP.NET Articles
More By Vadivel Mohanakrishnan