method One:
Copy CodeThe code is 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 CodeThe code is 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 CodeThe code is 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 CodeThe code is 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 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 ();
}
}