In our ASP.NET MVC Application, when session get timed out , page give crash on components where we have Jquery AJAX calls, however if we manually load the page it log out and redirect to login page.
We want user to redirect to login page automatically when session get expired, we are using form authentication and have a CustomPrincipal defined.
insert following code in global.asax
public class SessionExpireAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (HttpContext.Current.Session["SessionId"] == null)
{
filterContext.Result = new RedirectResult("~/Home/Login");
return;
}
base.OnActionExecuting(filterContext);
}
}
and in every controller call this attribute as following
[SolutionName.MvcApplication.SessionExpire]
public class HomeController : Controller
{
}