Note that there are some explanatory texts on larger screens.

plurals
  1. POThis page isn't redirecting properly error with MVC3 + AJAX + Forms Authentication
    primarykey
    data
    text
    <p>EDIT: Removed non-relevant code/desc, since the issue was not just to do with the initial code there. </p> <p>I have an MVC3 based application that uses a lot of Ajax calls (such as with jqGrid) and Forms Authentication. I also use the [Authorize] attribute on certain controller/actions that I call with Ajax in most cases. Every so often my application falls over with a 'This page isn't redirecting properly' or a 'this page has a redirect loop'.</p> <p>I checked this out with fiddler. After logging in as a user and trying to access pages that require authentication, sometimes I get redirected to Account/LogOn which then goes into an infinite loop. This usually happens when I'm calling any controller/action with an Authorize attribute with an Ajax call. The application seems to send out a 302 redirect to Account/Logon. The Account/Logon call then seems to redirect to itself. And the textView on Fiddler shows the following. </p> <pre><code>&lt;html&gt;&lt;head&gt;&lt;title&gt;Object moved&lt;/title&gt;&lt;/head&gt;&lt;body&gt; &lt;h2&gt;Object moved to &lt;a href="/Account/LogOn"&gt;here&lt;/a&gt;.&lt;/h2&gt; &lt;/body&gt;&lt;/html&gt; </code></pre> <p>I have the following in my Global.asax file </p> <pre><code>protected void Application_EndRequest() { var context = new HttpContextWrapper(this.Context); //if we are an ajax request and forms authentication caused a 302, then we actually need to do a 401 if (System.Web.Security.FormsAuthentication.IsEnabled &amp;&amp; context.Response.StatusCode == 302 &amp;&amp; context.Request.IsAjaxRequest()) { context.Response.Clear(); context.Response.StatusCode = 401; } } </code></pre> <p>And this in my main Layout page</p> <pre><code>&lt;script type="text/javascript"&gt; $(document).ajaxError(function (xhr, props) { if (props.status == 401) { location.reload(); } }); &lt;/script&gt; </code></pre> <p>My web.config setting for Forms authentication has this </p> <pre><code>&lt;authentication mode="Forms"&gt; &lt;forms loginUrl="~/Account/LogOn" timeout="2880"/&gt; &lt;/authentication&gt; </code></pre> <p>The redirect starts every so often and it is not with the same controller/action either. Just seems quite random. I am not sure if the cookie is expiry for the user and causing this issue for some reason or if it is an issue with the application pool recycling. Any suggestions on how to get around this would be most appreciated. Been struggling with this the last few days now, so any help from the experts will be great. Thank You</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload