asp.net mvc 3 - This page isn't redirecting properly error with MVC3 + AJAX + Forms Authentication -
edit: removed non-relevant code/desc, since issue not initial code there.
i have mvc3 based application uses lot of ajax calls (such jqgrid) , forms authentication. use [authorize] attribute on controller/actions call ajax in cases. every application falls on 'this page isn't redirecting properly' or 'this page has redirect loop'.
i checked out fiddler. after logging in user , trying access pages require authentication, redirected account/logon goes infinite loop. happens when i'm calling controller/action authorize attribute ajax call. application seems send out 302 redirect account/logon. account/logon call seems redirect itself. , textview on fiddler shows following.
<html><head><title>object moved</title></head><body> <h2>object moved <a href="/account/logon">here</a>.</h2> </body></html>
i have following in global.asax file
protected void application_endrequest() { var context = new httpcontextwrapper(this.context); //if ajax request , forms authentication caused 302, need 401 if (system.web.security.formsauthentication.isenabled && context.response.statuscode == 302 && context.request.isajaxrequest()) { context.response.clear(); context.response.statuscode = 401; } }
and in main layout page
<script type="text/javascript"> $(document).ajaxerror(function (xhr, props) { if (props.status == 401) { location.reload(); } }); </script>
my web.config setting forms authentication has
<authentication mode="forms"> <forms loginurl="~/account/logon" timeout="2880"/> </authentication>
the redirect starts every , not same controller/action either. seems quite random. not sure if cookie expiry user , causing issue reason or if issue application pool recycling. suggestions on how around appreciated. been struggling last few days now, experts great. thank you
decorate action method authorize attribute make available authenticated/logged in users, after check user role.
Comments
Post a Comment