how to get how many client login using the same user?

mc 3,701 Reputation points
2023-05-29T13:25:23.2166667+00:00

I am using cookie authentication.

one user can login in many clients.

I want to know how many login using the same user.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,188 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 56,686 Reputation points
    2023-05-29T15:29:48.4433333+00:00

    First add a unique tracking id to the cookie (say via a claim). Then on every request log the userid, tracking id, and cookie expiration. You can then sum the number of log entries where the cookie is not expired.

    0 comments No comments

  2. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2023-05-30T02:47:04.38+00:00

    Hi @mc

    In the Login page, after login success, you can insert the username and the login time into a data table (in the database, named LoginStatus), so when user login on multiple clients, the LoginStatus table have multiple records. And you should also insert the username and login time into the cookie (via the claims). Then, when user logout, in the logout handle method, get the username and its login time, after that remove the item from the LoginStatus table. By using this method, if you want to know how many clients are logged on with the same username, just query the LoginStatus table and filter the data based on the username.

    Note: It is easier to click the Logout button and use the HttpContext.SignOutAsync method to sign out the user and delete the cookie. But if cookie expired or user closed the window, in this scenario, you might need to use JavaScript to detect whether the cookie is expired, or user closed the window, then call the back-end method and use the HttpContext.SignOutAsync method to sign out.

    Besides, you can try to store the cookie expired time in the LoginStatus table, if user active, update the expired time the LoginStatus table, then when query the table, you can filter data based the username and the expired time.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Dillion

    0 comments No comments