locked
Is user logged in? RRS feed

  • Question

  • User1000945240 posted

    I have a function that contains the following code that checks a list of users of the site and not just the current user.

     'is user online?
            Dim u As MembershipUser
    
    
    
            u = Membership.GetUser(strMemberName, False)
            IsUserOnline = u.IsOnline

    It kinda sorta works.

    But in testing I have found that a user can log out of the site and still be seen as online according to the above.

    I don't see anything in  membership.getuser that checks for this.

    Is there a way to find out if another user other than the current one is logged in?

    Monday, July 13, 2015 11:57 AM

Answers

  • User-84896714 posted

    Hi thsman,

    Thank you for your post. Membership/Identity does not implement this function. You could implement by yourself.  You could store a collection of logged-in users in Application object. If user logged, add user to the collection, if user logout or session timeout, remove user from the collection. Code below is for your reference.

    Public Class LogedUsers
        Public Function GetLogedUsers() As List(Of String)
            If Not HttpContext.Current.Application("LogedUsers") Is Nothing Then
                Return TryCast(HttpContext.Current.Application("LogedUsers"), List(Of String))
            End If
            Return New List(Of String)()
        End Function
        Public Sub AddLogedUser(ByVal user As String)
            Dim users As List(Of String) = GetLogedUsers()
            users.Add(user)
            HttpContext.Current.Application("LogedUsers") = users
        End Sub
        Public Sub RemoveUser(ByVal user As String)
            Dim users As List(Of String) = GetLogedUsers()
            users.Remove(user)
            HttpContext.Current.Application("LogedUsers") = users
        End Sub
    End Class

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 14, 2015 2:03 AM

All replies

  • User-84896714 posted

    Hi thsman,

    Thank you for your post. Membership/Identity does not implement this function. You could implement by yourself.  You could store a collection of logged-in users in Application object. If user logged, add user to the collection, if user logout or session timeout, remove user from the collection. Code below is for your reference.

    Public Class LogedUsers
        Public Function GetLogedUsers() As List(Of String)
            If Not HttpContext.Current.Application("LogedUsers") Is Nothing Then
                Return TryCast(HttpContext.Current.Application("LogedUsers"), List(Of String))
            End If
            Return New List(Of String)()
        End Function
        Public Sub AddLogedUser(ByVal user As String)
            Dim users As List(Of String) = GetLogedUsers()
            users.Add(user)
            HttpContext.Current.Application("LogedUsers") = users
        End Sub
        Public Sub RemoveUser(ByVal user As String)
            Dim users As List(Of String) = GetLogedUsers()
            users.Remove(user)
            HttpContext.Current.Application("LogedUsers") = users
        End Sub
    End Class

    Best Regards,
    Wang Li

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, July 14, 2015 2:03 AM
  • User1000945240 posted

    That looks very interesting and promising. Thank you. I'll be trying it out in the next couple of days and will report back.

    Tuesday, July 14, 2015 5:29 PM