How to programmatically retrieve information from LDAP

I am running an ASP.Net page on IIS7 and developing in VS 2008. I am currently performing user authentication through an LDAP connection. When a user logs in, on one page they have a form with some basic information about them (for example, their name, email address, country, etc.), and I want to pre-fill some of these fields from the information already stored in LDAP. In particular, their name and email address. The question is, using C #, how do I really get this information?

+6
ldap
source share
1 answer

It looks like you are on .Net 3.5 SP1, in which case you can use the System.DirectoryServices.AccountManagement namespace, which greatly simplifies this.

Here is an example:

var pc = new PrincipalContext(ContextType.Domain, "mydomaincontroller"); var u = UserPrincipal.FindByIdentity(pn, userName); var email = u.EmailAddress; var name = u.DisplayName; 

Here is a complete list of properties that you can capture.

+8
source share

All Articles