Ajax Request Login Time-out Jump login page workaround

Source: Internet
Author: User
Tags httpcontext

In the filter to determine whether to log in, if not logged back 401 status

 public class Selfonlyattribute:actionfilterattribute {public override void OnActionExecuting (actionexecuting            Context filtercontext) {httpresponsebase response = FilterContext.HttpContext.Response;            Httprequestbase request = FilterContext.HttpContext.Request;            if (filterContext.HttpContext.User.Identity.IsAuthenticated) {//already logged in code} else {if (!request. Isajaxrequest ()) {string str = Request.                    Url.AbsoluteUri.ToLower ();                    String str2 = "/login"; Filtercontext.result = new Redirectresult (string.                Format ("{0}?returnurl={1}", str2, str)); } else {response.                SetStatus (401); } Filtercontext.result = new Httpunauthorizedresult (); This line guarantees that the code response of the action is no longer executed. End ();   This sentence must be added, otherwise return to the foreground status is always 200             Return }        }    }

  

Add error handling code to global JS

   $ ("div"). Ajaxerror (function (e,xhr,opt) {        alert (xhr.status+ "" + xhr.statustext);           if (xhr.status=401) {                //todo not logged in jump to login page                window.location.href = '/login ';              }  });

  

Ajax Request Login Time-out Jump login page workaround

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.