ASP. NET does not allow duplicate login code (site line)

Source: Internet
Author: User


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)

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.