title: Check if user is part of administrator group - C#
body: <p>I have code to verify if user is present in administrator group on local machine. The code works fine if user is directly present in administrator group</p>
  
  <pre><code>using (DirectoryEntry groupEntry = new DirectoryEntry("WinNT://./Administrators,group")) {
      foreach (object member in (IEnumerable)groupEntry.Invoke("Members"))
      {
          using (DirectoryEntry memberEntry = new DirectoryEntry(member))
          {
              if (memberEntry.Name.ToLower() == UserName.ToLower())
              {
                  IsUserAdmin = true;
                  break;
              }
          }
      } }
  </code></pre>
  
  <p>But the code fails if user is present in an AD group and that AD group is added in administrator group. Another case is user is part of nested AD group and the final AD group is added in administrator group.</p>
  
  <p>How can we check if user is part of administrator group when he is directly added and when related AD group is present?</p>
  
  <p>I want to make the code work on Windows Server 2008, 2008 R2 and 2012</p>