custom attributes - Asp.net MVC - How to check session expire for Ajax request -
we using ajax call across application- trying find out global solution redirect login page if session expired while trying execute ajax request. have coded following solution taking post - handling session timeout in ajax calls
not sure why in care event "handleunauthorizedrequest" not fired.
custom attribute:
[attributeusage(attributetargets.class | attributetargets.method)] public class checksessionexpireattribute :authorizeattribute { protected override void handleunauthorizedrequest(authorizationcontext filtercontext) { if (filtercontext.httpcontext.request.isajaxrequest()) { var url = new urlhelper(filtercontext.requestcontext); var loginurl = url.content("/default.aspx"); filtercontext.httpcontext.session.removeall(); filtercontext.httpcontext.response.statuscode = 403; filtercontext.httpcontext.response.redirect(loginurl, false); filtercontext.result = new emptyresult(); } else { base.handleunauthorizedrequest(filtercontext); } } }
using above custom attribute follow in controller action:
[nocache] [checksessionexpire] public actionresult getsomething() { }
ajax call(js part):
function getsomething() { $.ajax({ cache: false, type: "get", async: true, url: "/customer/getsomething", success: function (data) { }, error: function (xhr, ajaxoptions, thrownerror) { } }
web config authentication settings:
<authentication mode="forms"> <forms loginurl="default.aspx" protection="all" timeout="3000" slidingexpiration="true" /> </authentication>
i try check deleting browser cooking before making ajax call event "checksessionexpireattribute " not fired- idea please.
thanks,
@paul
i checked , tested code, looks clearly.. problem ajax call wrong..
i fix ajax code, try this..
function getsomething() { $.ajax({ cache: false, type: "get", async: true, url: "/customer/getsomething", success: function (data) { }, error: function (xhr, ajaxoptions, thrownerror) { } }); }
Comments
Post a Comment