4

How to determine if async request form ajax form was redirected? In my case request is redirected to login page if user's session is closed.

I tried to check arguments of OnComplete, OnSuccess and OnBegin events (OnFailure is not called) but no one helped.

Currently I have entire Login page embaded in the div of current page in case of session closing.

The only way I see how to awoid this - is code like this:

function onSuccessPost(a,b,c) {
   if (a.indexOf("<!DOCTYPE html>") == 0) {
      window.location = window.location;
   }
   // ...
}

But this solution seems a bit ugly.

Any ideas?

Serhiy
  • 4,040
  • 4
  • 32
  • 51

2 Answers2

0
public ActionResult PossibleRedirectAction(bool isRedirect = false)
{
    ViewBag.IsRedirect = isRedirect;
}

And place this code in action from which you are redirecting:

RedirectToAction("PossibleRedirectAction", new {isRedirect = true});
karaxuna
  • 25,822
  • 11
  • 76
  • 111
  • This solution would be applicable if I would redirect from my action manually. But in my case redirect to Log In page happens automatically. – Serhiy Jun 13 '12 at 08:58
  • Maybe you want to check returnUrl? – karaxuna Jun 13 '12 at 09:01
  • Well, there is an http response header "Location" added after request. But I cannot see where can I check it. – Serhiy Jun 13 '12 at 09:03
0

Don't redirect from controller actions that are invoked through AJAX. You could use JSON:

public ActionResult Foo()
{
    var url = Url.Action("Bar", "Baz");
    return Json(new { location = url }, JsonRequestBehavior.AllowGet);
}

and now in your AJAX success callback:

success: function(result) {
    window.location.href = result.location;
}

Obviously if you intend to always redirect from the client side after an AJAX request, this completely kills any benefit from AJAX. Simply invoke the controller action using a standard link => it's meaningless to use AJAX in this case.


UPDATE:

It seems that what you are trying to do is to intercept the redirect to the LogOn page when a forms authentication cookie has expired. Phil Haack blogged about this: http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx

In this article he illustrates how you could prevent the Forms Authentication module to automatically redirect you to the LogOn page but instead send a 401 status code which could be intercepted by your AJAX request and perform the redirect on the client.

Darin Dimitrov
  • 960,118
  • 257
  • 3,196
  • 2,876
  • In my case redirect to Log In page happens automatically. I have no redirect logic in my action. But I have an Authorize attribute on controller. – Serhiy Jun 13 '12 at 09:00
  • And also I don't need to always redirect after an AJAX request. Only when session has been finished. – Serhiy Jun 13 '12 at 09:01
  • 2
    OK, you should have stated this in your question as this is a very important detail. Take a look at this article for a very neat way to intercept this redirect condition: http://haacked.com/archive/2011/10/04/prevent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx You can now capture 401 errors in your AJAX requests and prevent the ASP.NET forms authentication module to automatically redirect you to the LogOn page when an action is requested through AJAX. – Darin Dimitrov Jun 13 '12 at 09:02
  • That is exactly what I'm searchiig for! Post this comment as an answer so I could accept it. – Serhiy Jun 13 '12 at 09:07