How to retreive ActiveDirectory information for a username

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 = "mydomain\user"; // 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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: