title: ASP.NET MVC & Angular: After session expires, Angular Factory returns HTML of Login Page instead of JSON object
body: <p>If the user is on a page for a long time and the session ends, if they proceed to make an AJAX call after the session is already expired.. instead of receiving the JSON object, it instead receives the HTML of the login page.</p>
  
  <p>Ideally I'm trying to make it so that it will redirect to a log in page.</p>
  
  <p>Is there any way i can detect this?</p>
  
  <p>I already have an ActionFilterAttribute that works for non-AJAX calls like so:</p>
  
  <pre><code>public class VerifySessionAttribute : ActionFilterAttribute
  {
      public override void OnActionExecuting(ActionExecutingContext filterContext)
      {
          var userId = filterContext.HttpContext.Session["UserId"];
          var userName = filterContext.HttpContext.Session["UserName"];
  
          if (userId == null || userName == null)
          {
              filterContext.Result = new RedirectResult(string.Format("/Account/Login"));
              return;
          }
  
          base.OnActionExecuting(filterContext);
      }
  }
  </code></pre>
  
  <p>But that doesn't get hit for the scenario above during AJAX calls.</p>
  
  <p>I've also tried an Interceptor.. something like this:</p>
  
  <pre><code>app.factory('httpAuthInterceptor', function ($q) {
      return {
          'responseError': function (response) {
              // NOTE: detect error because of unauthenticated user
              if ([401, 403].indexOf(response.status) &gt;= 0) {
                  // redirecting to login page
                  //$state.go('home');
                  $window.location.href = '/Account/Login';
                  return response;
              } else {
                  return $q.reject(rejection);
              }
          }
      };
  })
  
  app.config(function ($httpProvider) {
      $httpProvider.interceptors.push('httpAuthInterceptor');
  });
  </code></pre>
  
  <p>But in the same scenario it doesn't seem to hit there as well during the expired session / AJAX call</p>
  
  <p>Is there anything I can do to detect this?  When the session is expired I just want to redirect to the login page..  Thanks for any help</p>
  
  <p>EDIT:  here's how I make my calls</p>
  
  <pre><code>app.factory('HeadlinesFactory', ['$http', function ($http) {
      var HeadlinesFactory = {};
  
      HeadlinesFactory.getShowsForClient = function (clientId) {
          return $http({
              method: 'POST',
              url: '/Show/GetShowsForClient',
              data: { clientId: JSON.stringify(clientId) }
          });
      };
      //etc
  </code></pre>
  
  <p>EDIT2: how all my controllers look like.  Except my Account Controller where I put the VerifySession in front of everything except the Login page to prevent loop redirects:</p>
  
  <pre><code>[Authorize]
  [CustomFilters.VerifySession]
  public class ShowController : Controller
  { ... }
  </code></pre>