I just want to add one more thing that you can use, although this is also not accurate as Denis mentioned, there is IsOnline
property on the ASP.NET MembershipUser object but note that this relies on the LastActivityDate
which isn't updated that much as per:
The LastActivityDate for a user is updated to the current date and time by the CreateUser and ValidateUser methods, and can be updated by some overloads of the GetUser method. You can use the UpdateUser method to set the LastActivityDate property to a specific date and time.
The LastActivityDate is used to determine whether a user is online. A user is considered online if the current date and time minus the UserIsOnlineTimeWindow property value is earlier than the LastActivityDate for the user.
In some scenarios we are using a small service method which will update that value and call to that service method is scheduled on client-side so that LastActivityDate can be more accurate.
/// Updates users online status.
/// <param name="userIds">User ids.</param>
var userId = SecurityUtility.GetUserId();
Task.Factory.StartNew(() => GenericRepository.GetInstance().UpdateLastActivityDate(userId, DateTime.Now));
UpdateLastActivityDate(Guid userId, DateTime date)
AspnetUsersEntity user =
user.LastActivityDate = date.ToUniversalTime();
RelationPredicateBucket bucket =
bucket.PredicateExpression.Add(AspnetUsersFields.UserId == userId);