Friday, December 2, 2011

Retrieving personal details from Active Directory

If you are programming in C#, you can access information held on Active Directory easily using the code below.

Below uses the libraries provided in DirectoryServices namespace to query the Active Directory such as a user's telephone number, email address, etc.  As you can see, it is very straight forward (at least compared with tutorials I have seen using LDAP)

<% System.DirectoryServices.DirectoryEntry directoryEntry
= principal.GetUnderlyingObject() as
System.DirectoryServices.DirectoryEntry; %>


<%: principal.DisplayName %>

Tel: <%: directoryEntry.Properties["telephoneNumber"].Value%>

Email: <%: directoryEntry.Properties["mail"].Value%>


Although it looks like classes in DirectoryServices are wrappers to equivalent LDAP calls, it is certainly a lot cleaner and easier to understand (in my opinions)

The full list of properties you can retrieve can be found on

No comments: