To redirect the user to the login page when the session timed out for an Ajax request, I executed the following user attribute.
The code associated with the Unauthorize request is as follows:
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
filterContext.HttpContext.Response.StatusCode = 403;
filterContext.Result = new JsonResult
{
Data = new
{
Error = "SessionTimeOut"
},
JsonRequestBehavior = JsonRequestBehavior.AllowGet
};
filterContext.HttpContext.Response.End();
}
....................
This works great for ajax requests ($ .ajax).
But filterContext.HttpContext.Request.IsAjaxRequest () does not recognize the XMLHttp request as an ajax request.
var xhr = new XMLHttpRequest();
xhr.open('POST', "...URL");
xhr.send(formdata);
Has anyone encountered a similar problem? what would be the solution for this?
source
share