Right now I am using the 3.5 Framework to get the users first and last name, and email address.

username = System.DirectoryServices.AccountManagement.UserPrincipal.Current.GivenName + " " +
System.DirectoryServices.AccountManagement.UserPrincipal.Current.Surname;

I had to dumb the project down so it just uses the 2.0 Framework, and AccountManagement is not included.

Is there a simple way to get these things from AD with the 2.0 Framework. I have yet to find anything simple.

Recommended Answers

All 3 Replies

That link is helpful but what do I need to do to customize it for my program?

DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://CN=User1,OU=TechWriters,DC=Fabrikam,DC=Com";

// Display the value of the displayName attribute
MessageBox.Show((de.Properties["displayName"].Value).ToString());

What info is required to make it work? Say my email is JohnDoe@BTD.com???

I am now getting the error:
A referral was returned from the server.

Here is the code I have now. Does anyone have any idea about this?

string user = "Domain\\username";
            DirectoryEntry entry = new DirectoryEntry("LDAP://DC=Domain");
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            mySearcher.Filter = "(&(objectClass=user)(anr=" + user + "))";
            mySearcher.PropertiesToLoad.Add("mail");
            SearchResult searchResult = mySearcher.FindOne();
            string email = searchResult.Properties["mail"].ToString();
            MessageBox.Show(email);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.