Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine the number of users on an ASP.NET site (IIS)? And their info?

Is there a way to determine the number of users that have active sessions in an ASP.NET application? I have an admin/tools page in a particular application, and I would like to display info regarding all open sessions, such as the number of sessions, and perhaps the requesting machines' addresses, or other credential information for each user.

like image 247
Samuel Meacham Avatar asked Sep 30 '08 23:09

Samuel Meacham


People also ask

How can I see ASP net in IIS?

In Control Panel, click Programs, and then click Turn Windows features on or off. In the Windows Features dialog box, click Internet Information Services to install the default features. Expand the Application Development Features node and click ASP.NET 4.5 to add the features that support ASP.NET. (If you installed .

How does ASP Net work with IIS?

IIS has its own ASP.NET Process Engine to handle the ASP.NET request. So, when a request comes from client to server, IIS takes that request and process it and send the response back to clients. Hope, till now it's clear to you that what is the Web server and IIS is and what is the use of them.

How configure ASP in IIS?

Click Start, and then click Control Panel. In Control Panel, click Programs and Features, and then click Turn Windows Features on or off. Expand Internet Information Services, then World Wide Web Services, then Application Development Features. Select ASP, and then click OK.


1 Answers

In global.aspx

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    Application["OnlineUsers"] = 0;
}

void Session_Start(object sender, EventArgs e)
{
    // Code that runs when a new session is started
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
    Application.UnLock();
}

void Session_End(object sender, EventArgs e)
{
    // Code that runs when a session ends. 
    // Note: The Session_End event is raised only when the sessionstate 
    // mode is set to InProc in the Web.config file. 
    // If session mode is set to StateServer or SQLServer, 
    // the event is not raised.
    Application.Lock();
    Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
    Application.UnLock();
}

Note: The Application.Lock and Application.Unlock methods are used to prevent multiple threads from changing this variable at the same time.

In Web.config

Verify that the SessionState is "InProc" for this to work

    <system.web>
        <sessionState mode="InProc" cookieless="false" timeout="20" />
    </system.web>

In your .aspx file

Visitors online: <%= Application["OnlineUsers"].ToString() %>

Note: Code was originally copied from http://www.aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx (link no longer active)

like image 80
Eduardo Molteni Avatar answered Oct 10 '22 20:10

Eduardo Molteni