Thursday, May 31, 2007

Example of search in AD for the users of specific group

StringCollection users = new StringCollection();
DirectoryEntry directoryEntry = new DirectoryEntry("LDAP://ServerName:PortNumber/SubPath");
DirectorySearcher searcher = new DirectorySearcher(directoryEntry);

searcher.Filter = string.Format("(&(objectClass=user)(&(memberOf={0})))", groupName);

SearchResultCollection searchResults;
searchResults = searcher.FindAll();

foreach (SearchResult searchResult in searchResults)
{
ResultPropertyCollection rpC = searchResult.Properties;
if (rpC.Contains(_searchSchemaAttribute))
{
foreach (object obProp in rpC[_searchSchemaAttribute])
{
users.Add(obProp.ToString());
}
}
}

No comments: