ANGULARJS and ASP. NET MVC Login Timeout Solution

Source: Internet
Author: User
Tags httpcontext

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

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.