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!

Angelo CaixetaPosted Jan 27, 2015, 3:52 PM
Works perfectly, I can see the name of logged in users.But my access database has fields name, id and access level, and I need to show not only the user name as it is now, but I also want to show the password and access level. In other words, I need to show side by side -> Example: name | id | level munir | 1 | 3 I show like above. Now, just show the name: munir I picked up my database and transmit the information, but whosehandler only takes the name. I could not make that change, I could not edit the code to adapt and add more data in whosehandler. How i can do this? Thanks in advanced
Former memberPosted Oct 2, 2012, 2:39 AM
Hi....This is really nice article. http://dotnetpools.com/Article/ArticleDetiail/?articleId=24&title=Ajax%20Login%20Example%20Using%20Jquery%20%20In%20Asp.net
Matt EbaksPosted Jul 13, 2011, 1:52 AM
How can I print the online users in (for example: Label) instead in Page?
Lakhbir chandelPosted Jun 18, 2011, 7:20 AM
Thanks for Developing So Many easy Coding In C#..i m very Thankful to u sir.... god bless u ....
sajid khanPosted Jan 17, 2009, 7:58 AM
|There is one confusion in your code that will it maintain hastable over all applications
mat moPosted Dec 24, 2008, 4:58 PM
when i click 'download file' :/