string myLDAPPath = "";
try
{
// Create a 'DirectoryEntry' object to search.
Console.WriteLine("Enter the path ( Ex : 'LDAP://YourDomainName')");
myLDAPPath = Console.ReadLine();
DirectoryEntry mySearchRoot = new DirectoryEntry(myLDAPPath);
DirectorySearcher myDirectorySearcher = new DirectorySearcher(mySearchRoot);
// Get the first entry of the search.
SearchResult mySearchResult = myDirectorySearcher.FindOne();
if (mySearchResult != null)
{
// Get the 'DirectoryEntry' that corresponds to 'mySearchResult'.
DirectoryEntry myDirectoryEntry = mySearchResult.GetDirectoryEntry();
Console.WriteLine("\nThe name of the 'myDirectoryEntry' " + "directory entry that corresponds to the " + "'mySearchResult' search result is : {0}\n", myDirectoryEntry.Name);
string mySearchResultPath = mySearchResult.Path;
Console.WriteLine("The path for the 'mySearchResult' search " + "result is : {0}\n", mySearchResultPath);
// Get the properties of the 'mySearchResult'.
ResultPropertyCollection myResultPropColl;
myResultPropColl = mySearchResult.Properties;
Console.WriteLine("The properties of the " + "'mySearchResult' are :");
foreach(string myKey in myResultPropColl.PropertyNames)
{
string tab = " ";
Console.WriteLine(myKey + " = ");
foreach(Object myCollection in myResultPropColl[myKey])
{
Console.WriteLine(tab + myCollection);
}
}
mySearchRoot.Dispose();
myDirectoryEntry.Dispose();
}
else
{
Console.WriteLine("The '" + myLDAPPath + "' path not found.");
}
}
catch (Exception e)
{
Console.WriteLine("The '" + myLDAPPath + "' path not found.");
Console.WriteLine("Exception : " + e.Message);
}Given code is given for extracting active directory User details.
