The next account in asp.net does not allow multiple users to log on online at the same time.

Source: Internet
Author: User

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: // how to 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 codeCopy 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 logon 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 codeCopy 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 ();
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.