Authorize attribute not working, AJAX call executed even session is expired in asp.net mvc 4 -


i developing application using asp.net mvc 4. in application doing ajax calls functionality. actions in controller needs authentication write custom authorization code follows:

public class customlogin : authorizeattribute {     public override void onauthorization(authorizationcontext filtercontext)     {         /* checking user authenticated or not(user may become unauthenticated because of not logged-in system or session timeout) */         bool isauthenticated = httpcontext.current.user.identity.isauthenticated;          if (isauthenticated == true)         {            //do nothing         }         else         {             /* if user unauthenticated redirecting login page */                             handleunauthorizedrequest(filtercontext);         }     }      protected override void handleunauthorizedrequest(authorizationcontext filtercontext)     {         if (filtercontext.httpcontext.request.isajaxrequest())         {             /* if request ajax call setting status code return */             filtercontext.httpcontext.response.statuscode = 401;                                             filtercontext.httpcontext.response.end();         }         else         {                             /* if request not ajax call redirecting login page */             filtercontext.result = new redirecttorouteresult(                                         new routevaluedictionary{                                             { "controller", "login" },                                             { "action", "index" },                                             { "id", "sessionexpire" }                                       });         }     } } 

in controllers applied attribute follows:

[customlogin()] public class ordermanagercontroller : controller {    ........    actions comes here    ........ } 

and ajax code this:

$.ajax({ type: "post", url: myurlhere, success: function (response) {     if (response.result == "ok") {         /* here */     }                             }, error: function (xhr) {     if (xhr.status === 401) {         /* method redirect user login page */         redirecttologinpage();         return;     } } 

});

this code works both normal calls , ajax calls. issue when session expired , ajax call action method executes first, add/edit/delete whatever goes "error" callback of ajax call , redirect user login page. customlogin attribute works don't know why action method executed session expired. there way return error callback in ajax call without executing action method if session expired?

thanks...


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -