Guarav T.
Guarav T.

Reputation: 458

To redirect the user to login page automatically instead of crash on page in ASP.NET MVC

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.

Upvotes: 1

Views: 447

Answers (1)

Nijin P J
Nijin P J

Reputation: 1322

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
   {
   }

Upvotes: 2

Related Questions