Alicia Alicia - 1 year ago 57
jQuery Question

Preventing jQuery modal popup from opening on session timeout MVC4

I have an ASP.NET MVC 4 application, in which I'm trapping session timeout in a session timeout filter handler to handle session timeouts. I also want to handle session timeout for ajax requests. I initially implemented the code I found in this question here.

Which worked a treat initially for autocompletes and other ajax calls, but the only issue being for ajax calls to modal popups!

So now I have changed my session timeout handler to look like this :

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext.HttpContext.Session != null)
{
if (filterContext.HttpContext.Session.IsNewSession)
{
var sessionStateDetails =(SessionStateSection)ConfigurationManager.GetSection("system.web/sessionState");
var sessionCookie = filterContext.HttpContext.Request.Headers["Cookie"];

if ((sessionCookie != null) && (sessionCookie.IndexOf(sessionStateDetails.CookieName) >= 0))
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.Response.Clear();
filterContext.HttpContext.Response.StatusCode = 500;


i.e. setting the status code to 500 (401 didnt work for me cause I am using Windows authentication and impersonation so if I change status to 401, I get a security popup for password and username).

And then I trap this in either an .ajaxSetup , or .ajaxError I have tried both ...and re-direct to my session timeout action (as required) to display view with session timeout.

$(document).ajaxError(function (xhr, props) {
if (props.status === 500) {
////session redirect goes here
var pathArray = window.location.pathname.split('/');
var segment_1 = pathArray[1];
var newURL = window.location.protocol + "//" + window.location.host + "/" + segment_1 + "/Home/SessionTimeout";
window.location = newURL;
}
});


The problem is before the re-direct , the ajax popup is still briefly opened. So it doesnt look great. Any idea how I can prevent it opening at all and re-directing smoothly ?

Here is my jQuery dialog code:

$(document).ready(function () {
$(".openDialog").live("click", function (e) {
e.preventDefault();
$("<div></div>")
.addClass("dialog")
.attr("id", $(this)
.attr("data-dialog-id"))
.appendTo("body")
.dialog({
title: $(this).attr("data-dialog-title"),
close: function () { $(this).remove() },
width: 600,
modal: true,
height: 'auto',
show: 'fade',
hide: 'fade',
resizable: 'false'
})
.load(this.href);

});

$(".close").live("click", function (e) {
e.preventDefault();
$(this).closest(".dialog").dialog("close");
});
});


Any help would be appreciated, I think im almost there with this.

Answer Source

I managed to get this working in the end , if anyone else is having the same issue , here is what i did ... i kept using the session handler , but i now raise a 403 instead of a 500 , as raising a 500 was obviously wrong and had a few undesirable side effects:

Code from

                if (filterContext.HttpContext.Request.IsAjaxRequest())
                {
                    filterContext.HttpContext.Response.Clear();
                    filterContext.HttpContext.Response.StatusCode = 403;

                }
                else ...usual code goes here ....

And then i trap the ajax error as follows:

$(document).ajaxError(function (xhr, props) {
    if (props.status == 403 ) {
        ////session redirect goes here 
        var pathArray = window.location.pathname.split('/');
        var segment_1 = pathArray[1];
        var newURL = window.location.protocol + "//" + window.location.host + "/" + segment_1 + "/Home/SessionTimeout";
        window.location = newURL;
    }
});

Of course my popup still attempts to load for the briefest of moments before re-direct ,but my users dont seem to mind.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download