Method 1:
Copy codeThe Code is as follows:
String sKey = username. Text. ToString (). Trim (); // obtain the value of the given Key in the Cache.
String sUser = Convert. ToString (Cache [sKey]); // check for existence
If (sUser = null | sUser = String. Empty)
{
TimeSpan SessTimeOut = new TimeSpan (0, 0, System. Web. HttpContext. Current. Session. Timeout, 0, 0); // get the Session expiration time
HttpContext. current. cache. insert (sKey, sKey, null, DateTime. maxValue, SessTimeOut, System. web. caching. cacheItemPriority. notRemovable, null); // put the value into the cache to facilitate single-point Login
// Login successful
}
Else if (Cache [sKey]. ToString () = sKey) // if this account has been logged on
{
ClientScript. RegisterStartupScript (GetType (), "prompt", "<script> alert ('Sorry, the current user has logged on '); </script> ");
Return;
}
Else
{
Session. Abandon (); // This section is mainly used to prevent unnecessary errors and cause logon failure.
}
Copy codeThe Code is as follows:
// Clear the Cache when closing the browser or window. Add an onunload Event to the aspx file on the home page. Clear Session. SessionID in hOnline through ajax.
Window. onunload = function (){
$. Ajax ({
Type: "POST ",
Data: "sKey =" + sKey;
Url: "online. aspx"
});
}
Online. aspx. cs code
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
HttpContext. Current. Cache. Remove (sKey );
}
// Add the Session_End method in the Global. asax File
// After the Session expires, clear the Session. SessionID in hOnline.
Hashtable hOnline = (Hashtable) Application ["Online"];
If (hOnline [Session. SessionID]! = Null)
{
HOnline. Remove (Session. SessionID );
Application. Lock ();
Application ["Online"] = hOnline;
Application. UnLock ();
}
Method 2:
Copy codeThe Code is as follows:
// SKey is the Login User Name
If (ApplicationOnline (username. Text. tirm ())){
Hashtable hOnline = new Hashtable ();
HOnline [Session. SessionID] = sKey;
Application. Lock ();
Application ["Online"] = hOnline;
Application. UnLock ();
}
Public Boolean ApplicationOnline (string sKey)
{
Boolean flag = true;
Hashtable hOnline = (Hashtable) Application ["Online"];
If (hOnline! = Null)
{
IDictionaryEnumerator idE = hOnline. GetEnumerator ();
While (idE. MoveNext ())
{
// If (idE. Key! = Null & idE. Key. ToString (). Equals (Session. SessionID ))
//{
If (idE. Value! = Null & sKey. Equals (idE. Value. ToString ()))
{
Flag = false;
}
Break;
//}
}
}
Return flag;
}
// Clear Session. SessionID when closing the browser or window. Add an onunload Event to the aspx file on the home page. Clear Session. SessionID through ajax.
Window. onunload = function (){
$. Ajax ({
Type: "POST ",
Url: "online. aspx"
});
}
Online. aspx. cs code
Copy codeThe Code is as follows:
Protected void Page_Load (object sender, EventArgs e)
{
Hashtable hOnline = (Hashtable) Application ["Online"];
If (hOnline [Session. SessionID]! = Null)
{
HOnline. Remove (Session. SessionID );
Application. Lock ();
Application ["Online"] = hOnline;
Application. UnLock ();
}
}