Select Page
This entry has been published on 2012-05-10 and may be out of date.

Last Updated on 2012-05-10.

string principal = "meow"; //Windows Username

string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", principal);

string domain = "domainname.local";

string[] properties = new string[] { "fullname" };

DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domain, null, null, AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(adRoot);
searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter = filter;
SearchResult result = searcher.FindOne();
DirectoryEntry directoryEntry = result.GetDirectoryEntry();

string displayName = directoryEntry.Properties["displayName"][0].ToString();
string firstName = directoryEntry.Properties["givenName"][0].ToString();
string lastName = directoryEntry.Properties["sn"][0].ToString();
string email = directoryEntry.Properties["mail"][0].ToString();

byte [] pic = (byte []) directoryEntry.Properties["thumbnailPhoto"][0];

MemoryStream ms = new MemoryStream(pic);

pictureBox1.Image = Image.FromStream(ms);

Note:
To save pictures (e.g. user photos) in AD, you can use tools like this one.