How to retreive ActiveDirectory information for a username

By | 2006-02-09

Here is a little snippet to read some basic AD user info with C#. The UserADInfo class is a simple three property class.

using System.DirectoryServices;

UserADInfo info = new UserADInfo();
DirectoryEntry entry = new DirectoryEntry("LDAP://mydomain");
// Filter the domain out of the useraccount
String account = userAccount.Replace(@"mydomain", "");
try {
DirectorySearcher search = new DirectorySearcher(entry);
entry.Username = "mydomainuser"; // someone who has access
entry.Password = "password"

search.Filter = "(SAMAccountName=" + account + ")";
SearchResult result = search.FindOne();
if( result != null ) {
info.UserName = userAccount;
info.FullName = result.Properties["displayname"][0].ToString();
info.Email = result.Properties["mail"][0].ToString();
}
}
catch( Exception ex ) {
string debug = ex.Message;
throw;
}
return info;

Here are some of the AD/LDAP properties listed

http://www.computerperformance.co.uk/Logon/LDAP_attributes_active_directory.htm

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.