title: MVC Windows Authentication 401 on authorised pages
body: <p>I have an MVC5 website whereby I want anyone to be able to view the site apart from a restricted Admin area. I have created a filter to check the users AD group which I've applied to the admin controllers and this works absolutely fine.</p>
  
  <p>The issues is for people viewing the main site, if its idle for a while they get an unauthorised response upon navigating to a page. Once they refresh its fine and allows them back into the page.</p>
  
  <p>Web.Config has the following in</p>
  
  <pre><code>&lt;authentication mode="Windows" /&gt;
  </code></pre>
  
  <p>If I remove this then problem goes away but admin controllers don't work as there is no windows user found in my custom filter.</p>
  
  <p>The custom filter just checks for a AD group:</p>
  
  <pre><code> var groupList = GetGroupList();
  
          if (base.AuthorizeCore(httpContext))
          {
              if (string.IsNullOrEmpty(groupList))
                  return true;
  
              var groups = groupList.Split(',').ToList();
  
              var context = new PrincipalContext(ContextType.Domain, "MYDOMAIN");
  
              var userPrincipal = UserPrincipal.FindByIdentity(
                  context,
                  IdentityType.SamAccountName,
                  httpContext.User.Identity.Name);
  
              if (userPrincipal == null)
                  return false;
  
              try
              {
                  foreach (var group in groups)
                      if (userPrincipal.IsMemberOf(context,
                           IdentityType.Name,
                           group))
                          return true;
              }
              catch
              {
                  return false;
              }
  </code></pre>