title: Active directory : get groups where a user is member
body: <p>I'd like find the groups list where a user is member. I tried several solution from 
  <a href="http://www.codeproject.com/KB/system/everythingInAD.aspx" rel="nofollow">http://www.codeproject.com/KB/system/everythingInAD.aspx</a>
  but no result.</p>
  
  <p>This code give me a "true", means LDAP is running : </p>
  
  <pre><code>public static bool Exists(string objectPath)
  {
      bool found = false;
      if (DirectoryEntry.Exists("LDAP://" + objectPath))
          found = true;
      return found;
  }
  </code></pre>
  
  <p>Thanks,</p>
  
  <p><strong>Update 1:</strong></p>
  
  <pre><code>public ArrayList Groups(string userDn, bool recursive)
  {
      ArrayList groupMemberships = new ArrayList();
      return AttributeValuesMultiString("memberOf", "LDAP-Server",
          groupMemberships, recursive);
  }
  
  public ArrayList AttributeValuesMultiString(string attributeName,
  string objectDn, ArrayList valuesCollection, bool recursive)
  {
      DirectoryEntry ent = new DirectoryEntry(objectDn);
      PropertyValueCollection ValueCollection = ent.Properties[attributeName];
      IEnumerator en = ValueCollection.GetEnumerator();
  
      while (en.MoveNext())
      {
          if (en.Current != null)
          {
              if (!valuesCollection.Contains(en.Current.ToString()))
              {
                  valuesCollection.Add(en.Current.ToString());
                  if (recursive)
                  {
                      AttributeValuesMultiString(attributeName, "LDAP://" +
                      en.Current.ToString(), valuesCollection, true);
                  }
              }
          }
      }
      ent.Close();
      ent.Dispose();
      return valuesCollection;
  }
  </code></pre>
  
  <p>I have an exception on : </p>
  
  <pre><code>PropertyValueCollection ValueCollection = ent.Properties[attributeName];
  </code></pre>
  
  <p>"COMException was unhandled"</p>