title: Error: "An operations error occurred" in System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity
body: <p>I have the following code to retrieve AD groups of a given user name in my MVC3 web application:</p>
  
  <pre><code>PrincipalContext userDomain = new PrincipalContext(ContextType.Domain, username.Split('\\')[0]);
  UserPrincipal user = UserPrincipal.FindByIdentity(userDomain, username);
  PrincipalSearchResult&lt;Principal&gt; memberOfGroups = user.GetGroups();
  IEnumerator&lt;Principal&gt; memberOfGroupsEnumerator = memberOfGroups.GetEnumerator();
  List&lt;string&gt; userADGroups = new List&lt;string&gt;();
  
  try
  {
      while (memberOfGroupsEnumerator.MoveNext())
      {
          userADGroups.Add(memberOfGroupsEnumerator.Current.ToString());
      }
  }
  catch
  {
      // When trying to access AD groups of a different domain, issues can arise at the end of the enumerator. These may be ignored.
  
  }
  </code></pre>
  
  <p>This works fine locally but when deployed onto another machine on the network errors out with the following error:</p>
  
  <blockquote>
    <p>An operations error occurred.</p>
  </blockquote>
  
  <p>The stack trace for the error:</p>
  
  <blockquote>
    <p>System.DirectoryServices.DirectoryServicesCOMException (0x80072020): An operations error occurred.<br>
    at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)<br>
    at System.DirectoryServices.DirectoryEntry.Bind()<br>
    at System.DirectoryServices.DirectoryEntry.get_AdsObject()<br>
    at System.DirectoryServices.PropertyValueCollection.PopulateList()<br>
    at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry
    entry, String propertyName)<br>
    at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)<br>
    at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()<br>
    at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()<br>
    at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()<br>
    at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()<br>
    at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext
    context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)<br>
    at System.DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(PrincipalContext context, String identityValue)<br>
    at MvcSFIWebSite.Models.User..ctor(String username)</p>
  </blockquote>
  
  <p>The error message is rather ambiguous and I am unable to figure out what is happening as it works fine locally.</p>
  
  <p>The IIS on the machine used for deployment uses a custom account instead of the AppPool identity. Should this account be granted any permissions to access the AD group directory? Are any other settings explicitly required in IIS for this to work?</p>
  
  <p>Any suggestions would be very helpful. Thanks in advance.</p>