Skip to content

Instantly share code, notes, and snippets.

@jftuga
Last active October 13, 2020 02:31
Show Gist options
  • Select an option

  • Save jftuga/b50c31d3b77795a2397975da7cd3b222 to your computer and use it in GitHub Desktop.

Select an option

Save jftuga/b50c31d3b77795a2397975da7cd3b222 to your computer and use it in GitHub Desktop.
Retrieve Active Directory Contacts Belonging to a Group
/*
Given an AD group name, return all of the AD Contacts that belong to that group
Return a list of strings in this format:
CN Name|Email Address
*/
using System.DirectoryServices;
private List<string> GetGroupContacts(String ad_group_name)
{
DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE");
string domainContext = rootDSE.Properties["defaultNamingContext"].Value as string;
DirectoryEntry searchRoot = new DirectoryEntry("LDAP://" + domainContext);
String cn_group, look_for;
int j;
List<string> contacts = new List<string>();
using (DirectorySearcher searcher = new DirectorySearcher(searchRoot, "(&(objectCategory=person)(objectClass=contact))", new string[] { "cn", "mail", "memberof" }, SearchScope.Subtree))
{
foreach (SearchResult result in searcher.FindAll())
{
foreach (string member in result.Properties["memberof"])
{
for (j = 0; j < result.Properties["memberof"].Count; j++)
{
cn_group = result.Properties["memberof"][j].ToString();
look_for = "CN=" + ad_group_name + ",";
Console.WriteLine("cn_group: {0}", cn_group);
if (0 == cn_group.IndexOf(look_for))
{
Console.WriteLine(" Match found: {0}", result.Properties["cn"][0]);
contacts.Add(result.Properties["cn"][0] + "|" + result.Properties["mail"][0]);
}
}
}
}
}
return contacts;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment