Home > Backend Development > C++ > How Can I Efficiently Retrieve User Information from Active Directory Using C#?

How Can I Efficiently Retrieve User Information from Active Directory Using C#?

Mary-Kate Olsen
Release: 2025-01-06 13:23:40
Original
649 people have browsed it

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();
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template