title: ASP.NET - Get Security Groups for User
body: <p>I have a website that is running on the Intranet with Integrated Windows Security.</p>
  
  <pre><code>&lt;authentication mode="Windows" /&gt;
  &lt;identity impersonate="true" /&gt;
  &lt;authorization&gt;
      &lt;allow roles="MyDomain\Group1,MyDomain\Group2"/&gt;
  &lt;/authorization&gt;
  </code></pre>
  
  <p>How do I determine if the current user belongs to MyDomain\Group1 or MyDomain\Group2.  I tried this below, but it throws an error back:</p>
  
  <pre><code>PrincipalContext principalContext = new PrincipalContext(ContextType.Domain);
          GroupPrincipal group = GroupPrincipal.FindByIdentity(principalContext, "MyDomain\Group1");
          PrincipalSearchResult&lt;Principal&gt; user = UserPrincipal.Current.GetGroups();
  
          foreach (Principal principal in group.Members)
          {
              if (UserPrincipal.Current.Name == principal.Name)
              {
                  // Enable Functionality
  
                  break;
              }
          }
  </code></pre>
  
  <p>How do I determine if the current user belongs to Mydomain\Group1 or Mydomain\Group2?</p>