Membership.GetNumberOfUsersOnline Method ()


Gets the number of users currently accessing an application.

Namespace:   System.Web.Security
Assembly:  System.Web (in System.Web.dll)

public static int GetNumberOfUsersOnline()

Return Value

Type: System.Int32

The number of users currently accessing an application.

GetNumberOfUsersOnline returns the number of users for the current ApplicationName where the last-activity date is greater than the current time less the UserIsOnlineTimeWindow. The last-activity date/time stamp is updated to the current date and time when user credentials are validated by way of the ValidateUser or UpdateUser method or when a call to a GetUser overload that takes no parameters or one that uses the userIsOnline parameter to specify that the date/time stamp should be updated.

The following code example returns a list of users with a count of the number of users currently online.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

int pageSize = 5;
int totalUsers;
int totalPages;
int currentPage = 1;

public void Page_Load()
  if (!IsPostBack)

private void GetUsers()
  UsersOnlineLabel.Text = Membership.GetNumberOfUsersOnline().ToString();

  UserGrid.DataSource = Membership.GetAllUsers(currentPage-1, pageSize, out totalUsers);
  totalPages = ((totalUsers - 1) / pageSize) + 1;

  // Ensure that we do not navigate past the last page of users.

  if (currentPage > totalPages)
    currentPage = totalPages;

  CurrentPageLabel.Text = currentPage.ToString();
  TotalPagesLabel.Text = totalPages.ToString();

  if (currentPage == totalPages)
    NextButton.Visible = false;
    NextButton.Visible = true;

  if (currentPage == 1)
    PreviousButton.Visible = false;
    PreviousButton.Visible = true;

  if (totalUsers <= 0)
    NavigationPanel.Visible = false;
    NavigationPanel.Visible = true;

public void NextButton_OnClick(object sender, EventArgs args)
  currentPage = Convert.ToInt32(CurrentPageLabel.Text);

public void PreviousButton_OnClick(object sender, EventArgs args)
  currentPage = Convert.ToInt32(CurrentPageLabel.Text);

<html xmlns="" >
<title>Sample: Find Users</title>

<form id="form1" runat="server">
  <h3>User List</h3>

  Number of Users Online: <asp:Label id="UsersOnlineLabel" runat="Server" /><br />

  <asp:Panel id="NavigationPanel" Visible="false" runat="server">
    <table border="0" cellpadding="3" cellspacing="3">
        <td style="width:100">Page <asp:Label id="CurrentPageLabel" runat="server" />
            of <asp:Label id="TotalPagesLabel" runat="server" /></td>
        <td style="width:60"><asp:LinkButton id="PreviousButton" Text="< Prev"
                            OnClick="PreviousButton_OnClick" runat="server" /></td>
        <td style="width:60"><asp:LinkButton id="NextButton" Text="Next >"
                            OnClick="NextButton_OnClick" runat="server" /></td>

  <asp:DataGrid id="UserGrid" runat="server"
                CellPadding="2" CellSpacing="1"
    <HeaderStyle BackColor="darkblue" ForeColor="white" />



.NET Framework
Available since 2.0
Return to top