简体   繁体   中英

Asp.net Membership Logged in Users list

I used following in my application to find the list of logged in user. It gives the list of logged in user but when i close the browser and clear the browser history, it shows the user isonline status true. Please can any one tell me where i am wrong?

When User signIn am validating user and redirect to url,

if (user != null)
{
     if (Membership.ValidateUser(user.UserName, txtUserPassword.Text))
     {
        FormsAuthentication.SetAuthCookie(txtUserName.Text, false);
     }
}

List of logged in user code

 MembershipUserCollection allUsers = Membership.GetAllUsers();

 MembershipUserCollection filteredUsers = new MembershipUserCollection();

 bool isOnline = true;
 foreach (MembershipUser user in allUsers)
 {
    // if user is currently online, add to gridview list
   if (user.IsOnline == isOnline)
   {
      filteredUsers.Add(user);
   }
 }

Not sure why you had a negative down vote, this was a valid question. You also may never see this answer but others may have the same problem.

MembershipUser.IsOnline is not a very accurate check of whether or not a user is online. From the MSDN , a user is determined to be online:

if the current date and time minus the UserIsOnlineTimeWindow property value is earlier than the LastActivityDate for the user.

This means the user could log in and then log out 10 seconds later and still be considered online.

So in your case you were logging in, checking the count, logging out, closing the browser, clearing cache, etc., but the user's LastActivityDate still fell within the assumed window described above. What you can do is:

  • shorten the time window (which can produce some unwanted side effects)
  • when logging the individual out, update their LastActivityDate to a time that would make the above check invalid (eg, the current time UserIsOnlineTimeWindow )
  • update the LastActivityDate manually at certain key points (eg, if your site is bookstore, maybe updating the LastActivityDate when they add an item to the cart)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM