Pages

Wednesday, December 15, 2010

Get No. of Users On-line in ASP.net Membership

Hello all,
In our web application many times we need to display, that how many numbers of users are on-line, This is become quite easy if we are making use of ASP.Net Membership in our application. The Membership is having a function which directly gives us the no. of users on-line in the web site. "Membership.GetNumberOfUsersOnline(); " this function gives us the list of users on-line.

Here is a implementation of this code in c#


System.Collections.Generic.List<string> lstUserOnline = new System.Collections.Generic.List<string>();

int intUsersOnline = Membership.GetNumberOfUsersOnline();

foreach(MembershipUser usr in Membership.GetAllUsers())
{
if(usr.IsOnline)
{
lstUserOnline.Items.Add(usr.UserName);
}
}

//Bind the List to the ListBox
ListBox1.DataSource = lstUserOnline;
ListBox1.DataBind();

But there are some cases which are hard to track, that Suppose if any users have Closed his browser window without Logging out from the application, in that the Users remains On-line so it will also come under the list. So to avoid this issue up to some extent we can make use of logout time setting in the web config file, where we can easily set the time-out for a user, if he is not active any more. The entry will be like this.

<membership defaultProvider="SqlProvider"
userIsOnlineTimeWindow="20">
<providers>
<add name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="SqlServices"
enablePasswordRetrieval="true"
enablePasswordReset="false"
requiresQuestionAndAnswer="true"
passwordFormat="Encrypted"
applicationName="MyApplication" />
</providers>
</membership>


Hope You have like this small piece of code. Please feel free to post the comment or any issue regarding this code.


Thanks,
Anil Kumar Pandey
MVP (Visual C# 2009-2010)

No comments:

Post a Comment

Kontera