Resolve duplicate logins using the. NET identity ticket and the global global processing file
First Step login method incoming user name
private void Getonline (string Name)
{
Hashtable singleonline = (Hashtable) system.web.httpcontext.current.application["Online"];
if (Singleonline = = null)
Singleonline = new Hashtable ();
session["mysession"] = "Test";
SessionID
if (Singleonline.containskey (Name))
{
Singleonline[name] = Session.SessionID;
}
Else
Singleonline.add (Name, Session.SessionID);
var HttpCookie = new HttpCookie (formsauthentication.formscookiename);
var Expires = DateTime.Now.AddMinutes (30);
Httpcookie.expires = Expires;
Httpcookie.value = Formsauthentication.encrypt (new FormsAuthenticationTicket (0, Name, DateTime.Now, Expires, False, Name));
HTTPCONTEXT.RESPONSE.COOKIES.ADD (HttpCookie);
System.Web.HttpContext.Current.Application.Lock ();
system.web.httpcontext.current.application["Online"] = Singleonline;
System.Web.HttpContext.Current.Application.UnLock ();
}
Step two configure add tickets and Web. config
var HttpCookie = new HttpCookie (formsauthentication.formscookiename);
var Expires = DateTime.Now.AddMinutes (30);
Httpcookie.expires = Expires;
Httpcookie.value = Formsauthentication.encrypt (new FormsAuthenticationTicket (0, Name, DateTime.Now, Expires, False, Name));
HTTPCONTEXT.RESPONSE.COOKIES.ADD (HttpCookie);
Identity tickets can be placed in the underlying package for storing user names and ticket information.
After you add the ticket information, there may be a problem with User.Identity.Name not getting the value, which means that you do not have the appropriate parameters configured in your Web. config
The configuration is as follows under the <system.web> node
<authentication mode= "Forms" >
<forms loginurl= "/" timeout= "/>
</authentication>
<authorization>
<allow users= "*"/>
</authorization>
Step three create a new class to configure the filter class for MVC
public class Loginactionfilter:actionfilterattribute
{
public override void OnActionExecuting (ActionExecutingContext filtercontext)
{
Hashtable singleonline = (Hashtable) filtercontext.httpcontext.application["Online"];
Determine if the current SessionID exists
if (singleonline! = null && singleonline.containskey (filterContext.HttpContext.User.Identity.Name))
{
if (!singleonline[filtercontext.httpcontext.user.identity.name]. Equals (FilterContext.HttpContext.Session.SessionID))
{
Filtercontext.result = new Contentresult () {Content = "<script>if (Confirm (' Your account has been landed elsewhere, do you want to return to the landing page to log in again? ') {window.location.href= '/';} Else{window.close ();} </script> "};
}
}
Base. OnActionExecuting (Filtercontext);
}
}
Put this string of code into your filter class and then ok the filter is used to determine if there is a duplicate login situation, the filter how to use here to say, if there is a duplicate login, then execute the IF statement within the processing method, where the processing is a popup confirmation box, of course, you can also jump directly to the login address, See what needs to be changed.
ASP to resolve account repeat login issues