Create a Who's Online using .NET's Membership Provider!

Developing and maintaining a community website can sometimes be a daunting task.  A great deal of thought, and planning needs to go into creating content for your community to view, experience, and interact with.  One fun piece that you will find on a number of community webistes is a "Who's Online".  This is just a little bit of content that tells you how many registered users you have, how many registered users are online, how many guests are online, and the usernames of the registered users online. 

So today we are going to create a usercontrol that will display who's online, in either a Horizontal or Vertical Format.

Start off with adding a usercontrol to your project with a label, and then in the codebehind we want to create a property and enum to set the orientation of our control.

Public m_Orientation As orientation

Public Enum orientation As Integer
    Horizontal = 1
    Vertical = 2
End Enum

Public Property Orientate() As orientation
        Return m_Orientation
    End Get
    Set(ByVal value As orientation)
        m_Orientation = value
    End Set
End Property

Now the trick to this control is getting the count of our guests online.  We have a couple of options to do this, one of which would be to store the session in a table and then getting the count from the table.  However I didn't like going with that method.  To much work.  So what we are going to do is store a count in the cache each time a new session is started.  And decrease that count when the session ends.  So lets move over to the global.asax and set this up.

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    'Initialize user count 
    HttpContext.Current.Cache.Insert("UserCount", 0)
End Sub

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the session is started
    HttpContext.Current.Cache("UserCount") += 1
End Sub

Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the session ends
    HttpContext.Current.Cache("UserCount") -= 1
End Sub

Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
    'Dont forget to clean up
End Sub

Now heading back to our usercontrol lets do the work that builds the counts. To get the count of registered users online we call Membership.GetNumberOfUsersOnline, we then just take that count and subtract it from the "UserCount" we have stored in our cache to get the count of guests online. As you can see we are using Membership.GetAllUsers.Count to get our total registered users count and looping through GetAllUsers to get the  UserName of each member online. 

Private Sub GetWhosOnline()
    Dim delimeter As String = "<br />"
    Dim guestCount As Integer
    Select Case m_Orientation
        Case orientation.Horizontal
            delimeter = " | "
        Case orientation.Vertical
            delimeter = "<br />"
    End Select
    If (HttpContext.Current.Cache("UserCount") IsNot Nothing) Then
        guestCount = CInt(HttpContext.Current.Cache("UserCount").ToString()) - CInt(Membership.GetNumberOfUsersOnline.ToString)
    End If
    Me.lblWhosOnline.Text = String.Format("Total Registered: {0}{1}", Membership.GetAllUsers.Count, delimeter)
    Me.lblWhosOnline.Text += String.Format("Total Online: Registered ({0}): Guests({1}){2}", _
Membership.GetNumberOfUsersOnline.ToString, guestCount, delimeter) Me.lblWhosOnline.Text += "Who's Online: " For Each memberUser As MembershipUser In Membership.GetAllUsers If Membership.GetUser(memberUser.UserName).IsOnline Then Me.lblWhosOnline.Text += memberUser.UserName & ", " End If Next lblWhosOnline.Text = Left(lblWhosOnline.Text, Len(lblWhosOnline.Text) - 2) End Sub

Now all we need to do is to include the usercontrol on a page, and set the our property for how we want it displayed.

<uc1:ucWhosOnline id="UcWhosOnline1" runat="server" Orientate="Vertical"  />

And now you have a "Who's Online" usercontrol for your community website.



Technorati Tags: ,


  • Very cool, thank you. Just remember to add a page load event to call the GetWhosOnline Sub :)

  • Thanx its a easier approuch.

  • Very good. Thanks a million for posting.

    One enhancement I found was to modify the web.config so shorten the amount of time one had to be logged off before it was reflected in this.

    (I would logoff and still be show as being online.)

    I added this to the web.config file.

  • Hi Diamson,

    Your article is very very very good.
    But one problem im having is, if i put the usercontrol on the master page, everytime i refresh the page the counters for guest user keeps on incrementing and it shows all the registered users as online

    Can you tell me why and what have i done wrong??

  • 876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001876001

Comments have been disabled for this content.