Problem:
1. Method for judging Ajax requests in action Request.isajaxrequest () is always false
2. Return to front desk statuscode and Httpunauthorizedresult, reception received status or 200, indicating success
Solution Solution:
1. After checking some information, it is found that the background to determine whether the AJAX request is based on the request data headers X-requested-with to identify, according to the current said use angularjs$ HTTP Service request should be automatically added to the right AH (there are many people who respond to the use of jquery also have this problem, I did not try, do not know whether it is a common disease). So try to angularjs all AJAX requests before adding X-requested-with, here using $httpprovider.interceptors interception method to do unified processing.
2. I use the Actionfilter method in the background unified processing needs to verify the login, return to Httpunauthorizedresult, but the foreground receives the status is always 200. Two points need to be noted here: a) need to manually set response status code; b) must be executed after Setup is complete Response.End ()
Foreground request, Response Interceptor Code:
Zmapp.config (function($httpProvider) {$httpProvider. Interceptors.push ([' $rootScope ', ' $q ', ' $location ', ' $timeout ', function($rootScope, $q, $location, $timeout) {return { ' Request ':function(config) {//Processing AJAX requests (otherwise background isajaxrequest () always false)config.headers[' x-requested-with '] = ' XMLHttpRequest '; returnConfig | |$q. When (config); }, ' Requesterror ':function(rejection) {returnrejection; }, ' Response ':function(response) {returnResponse | |$q. When (response); }, ' Responseerror ':function(response) {Console.log (' Responseerror: ' +response); if(Response.Status = = = 401 | | response.status = = 403) {Abp.notify.error ("The session timed out, please login again!" "); $timeout (function() {window.location = "/login";}, 3000); return false; } Else if(Response.Status = = 500{$location. Path ('/error '); return false; } return$q. Reject (response); } }; }]);});
Background Actionfilter Code:
Public classLoginattribute:actionfilterattribute { Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {httpcontextbase context=Filtercontext.httpcontext; Httpresponsebase Response=FilterContext.HttpContext.Response; Httprequestbase Request=filterContext.HttpContext.Request; if(Context. session["User"] ==NULL) { if(Request. Isajaxrequest ()) response. Status="401 Session Timeout"; ElseResponse. Redirect ("/login"); Filtercontext.result=NewHttpunauthorizedresult ();//this line guarantees that the action code is no longer executedResponse. End ();//This sentence must be added, otherwise the return to the foreground status is always return; } Base. OnActionExecuting (Filtercontext); } }
ANGULARJS and ASP. NET MVC Login Timeout Solution