ASP to resolve account repeat login issues

Source: Internet
Author: User
Tags httpcontext

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

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.