SIGN UP MEMBER LOGIN:    
ARTICLE

Whose Online in ASP.NET 2.0

Posted by Munir Shaikh Articles | ASP.NET Programming August 06, 2007
I had problem in one of the project to show who is onlne & how to track multiple login/prevent from multiple login. I had gone back and forth so many time and finally am here with small snippet of code, I guess eveyone should read this article!
Reader Level:
Download Files:
 

Some time requirement could be like, client do not want more than one person to logged in to the system with the same name, different people suggest different solutions and all the solutions work fine till the user is loging to the system and logged out to the system, but if user closed browser without loging out from the system. In such case user has to wait for the Session to expire.

I have given thougth so many time, and here I am with simple solution.

I have simple table called as Tbl_Users with following fields.

UId  int Primary key,
LoginName varchar(50),
LoginPassword varchar(50)

I am using C# Collection ie. HashTable. By using static hashtable if user is logging to the system very first time then I am adding users key value pair to the hashtable and while logging to the system I check programmatically that similar entry is available in the hashtable and prompt user with the message. I also allow user to kill users session explicitly, or let user to wait till the session expires.

To achive this functionality I used of seal class which is as below

using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

using System.Collections;

/// <summary>

/// Summary description for WhoseHandler

/// </summary>

public sealed class WhoseHandler

{

    public static Hashtable objUser = new Hashtable();

    /**

     * Function to check if user is already inn, else will add into the

     * Whose online list

     **/

    public static int WhoseInn(int userId, string strUser)

    {

        int retVal = 0;

        if (!(objUser.Contains(userId)))

        {

            objUser.Add(userId, strUser);

            return retVal;

        }

        else

        {

            retVal = 1;

        }

        return retVal;

    }

    /**

     * List All the users who are online

     **/

    public static void WhoseOnline()

    {

        IDictionaryEnumerator whosOln = objUser.GetEnumerator();

        while (whosOln.MoveNext())

        {

            HttpContext.Current.Response.Write(whosOln.Value + "<br>");

        }

    }

    /**

     * Remove user explicitly from the list

     **/

    public static void RemoveUser(int userId)

    {

        if (objUser.Contains(userId))

        {

            objUser.Remove(userId);

        }

    }

}

I am attaching entire code zip with the article.

Enjoy easy coding!

Login to add your contents and source code to this article
share this article :
post comment
 

How can I print the online users in (for example: Label) instead in Page?

Posted by Matt Ebaks Jul 13, 2011

Thanks for Developing So Many easy Coding In C#..i m very Thankful to u sir.... god bless u ....

Posted by Lakhbir chandel Jun 18, 2011

|There is one confusion in your code that will it maintain hastable over all applications

Posted by sajid khan Jan 17, 2009

when i click 'download file' :/

Posted by mat mo Dec 24, 2008
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor