C# / .NETDevOpsMisc
C# / .NET
C# Active Directory
Alexandru Puiu
Alexandru Puiu
September 16, 2012
1 min

C# has very good integration with Active Directory, and you can query all the objects you need. First, you’ll need to reference and import the following libraries:

using System.DirectoryServices; using System.DirectoryServices.AccountManagement;

.Net has a UserPrincipal built-in class, which will allow you quick access to common objects such as the Name and User Principal name

 // create your domain context
 PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
 // define a "query-by-example" principal - here, we search for a UserPrincipal
 // and with the first name (GivenName) of "Bruce"
 UserPrincipal qbeUser = new UserPrincipal(ctx);
 qbeUser.GivenName = "Steve";
 // create your principal searcher passing in the QBE principal
 PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
 // find all matches
 foreach(var found in srch.FindAll())
   // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....

Then you can start having some fun consuming all the resources of Active Directory. Here’s an example of how to retrieve the person’s Name, Display Name, Depatment and location of their office through LDAP.

DirectoryEntry entry = new DirectoryEntry("LDAP://DC=mydomain,DC=local");
using (DirectorySearcher ds = new DirectorySearcher(entry))
    ds.Filter = "(SAMAccountName=" + "myusername" + ")";
    var result = ds.FindOne();
    if (result != null)
            user.Name = result.Properties["name"][0].ToString();
            user.Department = result.Properties["department"][0].ToString();
            user.Office = result.Properties["physicalDeliveryOfficeName"][0].ToString();
        catch { }

You can download a full list of Active Directory properties

Spreadsheet of User Properties in Active Directory Users & Computers

Documents the attributes corresponding to the fields on the following tabs of the user properties dialog of ADUC: General, Address, Account, Profile, Telephones, and Organization.

Spreadsheet of all Active Directory attributes

Documents all attributes in a default installation of Windows Server 2008 R2 Active Directory. Does not include attributes added to the schema by Exchange. Indicates the syntax of each attribute in the schema, which are replicated to the Global Catalog, which are indexed, which are “constructed” (operational), which are not replicated, whether they are single or multi-valued, and which class of objects can use each attribute. The spreadsheet indicates which attributes were available in Windows 2000 Server and which were new in Windows Server 2003 or Windows Server 2008. Some of the attributes that are shown as not available in Windows Server 2003, but available in Windows Server 2008, where introduced in Windows Server 2003 R2.

Spreadsheet of User Object Property Methods

Documents all property methods available for user objects, which of these are supported by WinNT, the syntax, and the attributes they are based on. The value returned by a property method is not stored in Active Directory, but is calculated from other attributes.


Alexandru Puiu

Alexandru Puiu

Engineer / Security Architect

Systems Engineering advocate, Software Engineer, Security Architect / Researcher, SQL/NoSQL DBA, and Certified Scrum Master with a passion for Distributed Systems, AI and IoT..



Social Media


Related Posts

RavenDB Integration Testing
Using RavenDB in Integration Testing
December 24, 2022
2 min

Subscribe To My Newsletter

I'll only send worthwhile content I think you'll want, less than once a month, and promise to never spam or sell your information!
© 2023, All Rights Reserved.

Quick Links

Get In TouchAbout Me

Social Media