views:

225

answers:

2

I am using asp.net mvc and jquery to make ajax requests and when the session times out after an ajax request the full sigin page gets loaded into my ajax div.

How can I display a modal popup instead of making a redirect when a session times out?

+2  A: 

One way to handle this that comes to mind is to use a custom HTTP header:

$.ajax({
    url: '/home/someaction',
    data: {},
    success: function(html, status, xhr) {
        if (xhr.getResponseHeader('REQUIRES_AUTH') === '1') {
            alert('SESSION EXPIRED!!!');
        } else {
            $('#result').html(html);
        }
    }
});

And in your LogOn action you set the appropriate header:

public ActionResult LogOn()
{
    Response.AddHeader("REQUIRES_AUTH", "1");

    return View();
}
Darin Dimitrov
Thanks for the comment. I did try your solution and it was helpful in getting me on the right start. I ended up finding this plugin to do what I needed. http://www.erichynds.com/jquery/a-new-and-improved-jquery-idle-timeout-plugin/
Victor
A: 

My solution is just five lines of JavaScript

    $(document).ready(function () {
        if ($("#site").length > 0) {
            window.location = "<%= Url.Content("~") %>" + "Login/LogOn";
        }
    });

Put it on the login page. If is was loaded in a div on the main page, it will redirect til the login page. "#site" is a id of a div which is located on all pages except login page.

podeig