Can any one help me. I've been looking on how to count how many users are login to my site but using asp.net - c#.
Asked
Active
Viewed 3,091 times
0
-
Take a look at http://stackoverflow.com/questions/764976/asp-net-tracking-code-unique-visitors – dash Dec 01 '11 at 13:21
-
@dash .. that has nothing to do with online users that I can see? – Kieren Johnstone Dec 01 '11 at 13:25
-
Sorry, I misread the intent of the question. For the number of active users, you can just use the Session_OnStart (+1 to a counter) and Session_OnEnd (-1 to a counter) events I guess. This will at least count the number of active sessions which should equate to the number of users online (but not necessarily active!) – dash Dec 01 '11 at 13:28
-
This is a more approrpriate link I guess: http://stackoverflow.com/questions/6218401/how-to-count-sessions-in-asp-net-server-application – dash Dec 01 '11 at 13:30
-
As @dash suggested, this should work for you. you might like to handle Application_Error as well in case if there is an exception. Also, lock Application["LiveSessionsCount"] (from the above example) before making any change to this Application object. – Junaid Dec 01 '11 at 13:56
3 Answers
2
If you are using Membership, you can simply call Membership.GetNumberOfUsersOnline().

jrummell
- 42,637
- 17
- 112
- 171
1
In case you don't use membership(as jrummell suggested):
You will have to implement your own counter as far as I know.
Just add an integer to the Application dictionary and increment it every time somebody logs in.
Decrement it in logoff and in the Session_End event(don't forget to check that the session that just ended had an active login - of course.

Svarog
- 2,188
- 15
- 21
-
This will not work. Session_End will not always be called when a session ends. What if the app crashes for example? – Kieren Johnstone Dec 01 '11 at 13:26
-
If you use Application state then remember to wrap it in calls to `Lock()` and `Unlock()` to keep it thread-safe. Also be aware that this will only work for a single server and not on a web farm. – Chris Fulstow Dec 01 '11 at 13:26
-
@ChrisFulstow: There's also the [Increment](http://msdn.microsoft.com/en-us/library/dd78zt0c.aspx) method for simple cases like this. – Roman Dec 01 '11 at 13:54
-
@KierenJohnstone if the app crashes - all the sessions will die anyway. – Svarog Dec 01 '11 at 14:12
-
@ChrisFulstow increment is an atomic operation, there should be no thread-safety issues here. – Svarog Dec 01 '11 at 14:12
0
Here's a tool to know that refer to Online active users counter in ASP.NET. Its easy to install. It had worked well for me.

Sai Kalyan Kumar Akshinthala
- 11,704
- 8
- 43
- 67