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