Skip to main content

Preventing concurrent logins using same credentials

A few days back I faced a problem which made me go back and forth a couple of times over the code which I had written, and my work process was virtually ceased. The problem was regarding user login, i.e. “How can I be able to prevent concurrent user login using the same credentials from different machines”
Well, as I have found the solution I would like to share it with you all.



If we are using Membership Providers to store the user credentials, then we can use the IsOnline property of MemebershipUser Class to check if it’s a concurrent login or not. If the user logins with a username and password, then it is checked that the user is online or not, if he / she is not online(that means there is no concurrent login) then he / she is allowed, otherwise the user is redirected to another page.
Add this code to the LoggedIn() event of the Login control, which is raised after the user is authenticated  or in you Login Button click.
 
 

protected void logctrlLoginPage_LoggingIn(object sender, EventArgs e)
        {
            MembershipUser GetUser = Membership.GetUser(User.Identity.Name, false);
            if (GetUser.IsOnline)
            {
                Response.RedirectPermanent("~/NotAllowed.aspx");
            }
        }


And make changes to the Membership tag in web.config as this,

 

<membership defaultProvider="AspNetSQlMembershipProvider" userIsOnlineTimeWindow="1">


A user is considered online if the current date and time minus the UserIsOnlineTimeWindow property value is earlier than the LastActivityDate for the user. The LastActivityDate for a user is updated to the current date and time by the CreateUser, UpdateUser and ValidateUser methods, and can be updated by some of the overloads of the GetUser method.

Comments

  1. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.
    full stack developer training in chennai

    ReplyDelete
  2. Learn Oracle PLSQL for making your career towards a sky-high with Infycle Technologies. Infycle Technologies is the top Oracle PLSQL Training Institute in Chennai, offering programs in Oracle such as Oracle PL/SQL, Oracle DBA, etc., in the 200% hands-on practical training with professional specialists in the field. In addition to that, the interviews will be arranged for the candidates, so that, they can set their career without any struggle. Of all that, 100% placement assurance will be given here. To have the best career, call 7502633633 to Infycle Technologies and grab a free demo to know more.

    https://infycletechnologies.com/oracle-plsql-training-in-chennai

    ReplyDelete
  3. very information article.thanks for sharing.Angular training in Chennai

    ReplyDelete
  4. GetUser.IsOnline is true even after log out.

    ReplyDelete

Post a Comment