1

Trying to find solution for redirection to the login page after Ajax-call if a user is not authenticated longer.

I used a method described here How to manage a redirect request after a jQuery Ajax call

private static void RedirectionToLogin(ActionExecutingContext filterContext)
{
    string redirectOnSuccess = filterContext.HttpContext.Request.Url.AbsolutePath;
    string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess);
    string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;

    filterContext.HttpContext.Response.AddHeader("REQUIRES_AUTH", "1");
    filterContext.HttpContext.Response.StatusCode = 401;
    filterContext.HttpContext.Response.Redirect(loginUrl, true);
}

JavaScript is

$(window).ajaxComplete(function (event, request, settings) {
    alert(request.getResponseHeader('REQUIRES_AUTH'));
    alert(request.status);
    if (request.getResponseHeader('REQUIRES_AUTH') === 1) {
        window.location = App.basePath + "Login/LogOn";
    {
});

If user was logged out request.getResponseHeader('REQUIRES_AUTH')) is always NULL

alt text

Why request.getResponseHeader('REQUIRES_AUTH')) is NULL???

Community
  • 1
  • 1
podeig
  • 2,597
  • 8
  • 36
  • 60

1 Answers1

0

I think the reason you are getting a null value for "REQUIRES_AUTH" is that your log in page

    ~/Login/Logon 

does not require authentication. Which is correct, otherwise you would not be able to log in! Also your javascript has a curly bracket the wrong way around. (typo) should be:

$(window).ajaxComplete(function (event, request, settings) {

    alert(request.getResponseHeader('REQUIRES_AUTH'));

    alert(request.status);

    if (request.getResponseHeader('REQUIRES_AUTH') === 1) {
        window.location = App.basePath + "Login/LogOn";
    }
});

I am also looking for a nice solution for this problem, but am still a bit stuck. Have you managed to solve this problem? if so, how did you manage it?

Dai Bok
  • 3,451
  • 2
  • 53
  • 70
  • Thank you for your comment. I found a nice solution which works 100%. I posted it here http://stackoverflow.com/questions/199099/how-to-manage-a-redirect-request-after-a-jquery-ajax-call/3861414#3861414 I hope it will help you! :) – podeig Nov 05 '10 at 08:39