Home >Backend Development >C++ >How Can I Efficiently Retrieve User Information from Active Directory Using C#?
Retrieving User Information from Active Directory
As a novice to Active Directory, understanding its hierarchical data storage mechanism and LDAP query capabilities is paramount.
Using PrincipalSearcher from System.DirectoryServices.AccountManagement, one can efficiently retrieve user information. An example is provided below:
using (var context = new PrincipalContext(ContextType.Domain, "yourdomain.com")) { using (var searcher = new PrincipalSearcher(new UserPrincipal(context))) { foreach (var result in searcher.FindAll()) { DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry; Console.WriteLine("First Name: " + de.Properties["givenName"].Value); Console.WriteLine("Last Name : " + de.Properties["sn"].Value); Console.WriteLine("SAM account name : " + de.Properties["samAccountName"].Value); Console.WriteLine("User principal name: " + de.Properties["userPrincipalName"].Value); Console.WriteLine(); } } } Console.ReadLine();
Attributes like "givenName" provide the first name, "sn" provides the last name, "samAccountName" is the pre-Windows 2000 user logon name, and "userPrincipalName" is typically utilized post-Windows 2000.
The above is the detailed content of How Can I Efficiently Retrieve User Information from Active Directory Using C#?. For more information, please follow other related articles on the PHP Chinese website!