String SKey = Username. Text.tostring (). Trim (); Get the value of the given key in the cache
String suser = Convert.ToString (Cache[skey]); Check for Presence
if (suser = = NULL | | suser = = String.Empty)
{
TimeSpan sesstimeout = new TimeSpan (0, 0, System.Web.HttpContext.Current.Session.Timeout, 0, 0);//Get Session Expiration Time
HttpContext.Current.Cache.Insert (SKey, SKey, NULL, DateTime.MaxValue, Sesstimeout, System.Web.Caching.CacheItemPriority.NotRemovable, NULL);//Place the value in the cache and click
Successful Login
}
else if (Cache[skey]. ToString () = = SKey)//If this account is already logged in
{
Clientscript.registerstartupscript (GetType (), "hint", "<script>alert (' Sorry, current user is logged in ');</script>");
Return
}
Else
{
Session.Abandon ();//This paragraph is mainly to avoid unnecessary errors that can not log in
}
The method of emptying the cache when the browser or window is closed. Add a onunload event to the main page aspx file. Empty the Session.SessionID in Honline via Ajax
Window.onunload=function () {
$.ajax ({
Type: "POST",
Data: "skey=" +skey;
URL: "Online.aspx"
});
}
Online.aspx.cs of this page
protected void Page_Load (object sender, EventArgs e)
{
HttpContext.Current.Cache.Remove (SKey);
}
In the Session_End method in the Global.asax file, add
After the session expires. Empty 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 Two:
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; }
The way to empty the Session.SessionID when you close the browser or window. Add a onunload event to the main page aspx file. Empty Session.SessionID Window.onunload=function via Ajax ( {$.ajax ({type: "POST", url: "online.aspx"});
Online.aspx.cs of this page
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 ();
}
}
ASP. NET does not allow duplicate login code (site line)