Skip to content

Instantly share code, notes, and snippets.

@rstackhouse
Last active December 20, 2015 00:09
Show Gist options
  • Save rstackhouse/6039172 to your computer and use it in GitHub Desktop.
Save rstackhouse/6039172 to your computer and use it in GitHub Desktop.
.NET LDAP Search
DirectoryEntry rootEntry = new DirectoryEntry("LDAP://some.ldap.server.com");
rootEntry.AuthenticationType = AuthenticationTypes.None; //Or whatever it need be
DirectorySearcher searcher = new DirectorySearcher(rootEntry);
searcher.Filter = "(&(objectClass=user)(objectCategory=person)(SAMAccountName=some_user_name)";
foreach(SearchResult result in searcher.FindAll())
{
Console.WriteLine("account name: {0}", result.Properties["samaccountname"].Count > 0 ? result.Properties["samaccountname"][0] : string.Empty);
Console.WriteLine("common name: {0}", result.Properties["cn"].Count > 0 ? result.Properties["cn"][0] : string.Empty);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment