asp.net next account does not allow multiple users at the same time online, repeated login code _ Practical Skills

Source: Internet
Author: User
Tags httpcontext

method One:

Copy Code code as follows:

String SKey = Username. Text.tostring (). Trim (); Gets the value of the given key in the cache
String suser = Convert.ToString (Cache[skey]); Check to see if there is
if (suser = null | | suser = = String.Empty)
{
TimeSpan sesstimeout = new TimeSpan (0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);//Get the expiration time of session
HttpContext.Current.Cache.Insert (SKey, SKey, NULL, DateTime.MaxValue, Sesstimeout, System.Web.Caching.CacheItemPriority.NotRemovable, NULL);//Put the value into the cache and then single sign-on
Successful Login
}
else if (Cache[skey). ToString () = = SKey)//If this account is already logged in
{
Clientscript.registerstartupscript (GetType (), "hint", "<script>alert (' Sorry, current user has logged in ');</script>");
Return
}
Else
{
Session.Abandon ()//This paragraph is mainly to avoid unnecessary errors caused by the inability to log in
}

Copy Code code as follows:

The method of emptying the cache when the browser or window is closed. Adds a onunload event to the main page aspx file. Clear the Session.SessionID in the Honline with Ajax
Window.onunload=function () {
$.ajax ({
Type: "POST",
Data: "skey=" +skey;
URL: "Online.aspx"
});
}

Online.aspx.cs Code
Copy Code code as follows:

protected void Page_Load (object sender, EventArgs e)
{
HttpContext.Current.Cache.Remove (SKey);
}
Add in the Session_End method in the Global.asax file
After the session expires. Empty the Session.SessionID in the Honline.
Hashtable honline = (Hashtable) application["Online"];
if (Honline[session.sessionid]!= null)
{
Honline.remove (Session.SessionID);
Application.Lock ();
application["Online"] = Honline;
Application.UnLock ();
}

Method Two:
Copy Code code as follows:

Skey for 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;
}

The method of emptying the Session.SessionID when the browser or window is closed. Adds a onunload event to the main page aspx file. Empty Session.SessionID via Ajax
Window.onunload=function () {
$.ajax ({
Type: "POST",
URL: "Online.aspx"
});
}

Online.aspx.cs Code
Copy Code code 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.