title: Redirecting to Login Page Automatically without doing Action in Controller ASP.NET MVC when session expired
body: <p>I know it's a common question but I'm really exhausted of searching a complete solution without success. I have done an attribute filter but this works only if the user interact with the application and I don´t want this, I want when session expired, automatically the browser load the login page. Maybe someone could show what code should I use in javasccript or html .... and the configuration that I should use in the web.config if necesary. Thanks in advance.</p>
  
  <p>This is my actual attribute:</p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Method)]
      public class SessionExpireCustom : FilterAttribute, IActionFilter
      {
          public void OnActionExecuting(ActionExecutingContext filterContext)
          {
              if (HttpContext.Current.Session[HttpContext.Current.Session.SessionID + "_authstatus"] == null)
              {
                  string redirectTo = "~/Account/Login";
                  if (!string.IsNullOrEmpty(HttpContext.Current.Request.RawUrl))
                  {
                      redirectTo = string.Format("~/Account/Login?ReturnUrl={0}", HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl));
                      filterContext.Result = new RedirectResult(redirectTo);
                      return;
                  }
  
                  filterContext.Result = new RedirectResult("~/Account/Login");
                  return;
              }
          }
  
          public void OnActionExecuted(ActionExecutedContext filterContext)
          {
              return;
          }
      }
  </code></pre>
  
  <p>And in my web.config:</p>
  
  <pre><code>&lt;system.web&gt;
      &lt;sessionState mode="InProc" cookieless="false" timeout="20" /&gt;
      &lt;compilation debug="true" targetFramework="4.6.1" /&gt;
      &lt;httpRuntime targetFramework="4.6.1" maxRequestLength="102400"/&gt;
      &lt;!--KiloBytes--&gt; 
      &lt;httpModules&gt;
        &lt;add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /&gt;
      &lt;/httpModules&gt;    
    &lt;/system.web&gt;
  </code></pre>