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" + ")"; ds.PropertiesToLoad.Add("displayName"); ds.PropertiesToLoad.Add("name"); ds.PropertiesToLoad.Add("department"); ds.PropertiesToLoad.Add("physicalDeliveryOfficeName"); var result = ds.FindOne(); if (result != null) { try { 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 & ComputersDocuments 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 attributesDocuments 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 MethodsDocuments 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.
Quick Links
Legal Stuff