tags:

views:

351

answers:

1

I am running an ASP.Net page on IIS7, and developing in VS 2008. Currently, I have user authentication being done through an LDAP connection. Once the user logs in, on one page they have a form with some basic information about them (such as their name, email address, country, and the like) and I wish to pre populate some of these fields from information already stored in the LDAP. In particular their given name and email addresses. The question is, using C#, how do I actually retrieve this information?

+1  A: 

Sounds like you're on .Net 3.5 SP1, in that case you can use the System.DirectoryServices.AccountManagement namespace that greatly simplifies this.

Here's a sample:

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

Here's a full list of properties you can grab.

Nick Craver
Thanks, a small question though. I am unsure about what to use for the string you've used 'mydomaincontroller' for. The ldap authentication has been done by using ActiveDirectoryMembershipProvider in the Web.Config . Presumably I would have to provide the LDAP url as long as a username and password. When/Where/How is this done?
Jacob Bellamy
@Jacob - I normally connect to a variety of domain controllers...you can also just do `var pc = new PrincipalContext(ContextType.Domain);` to grab whatever domain controller windows picks or...alternatively, you can do `var pc = new PrincipalContext(ContextType.Domain, "my.server.com", "user", "pass");` Here's a full list of options: http://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.principalcontext.principalcontext.aspx
Nick Craver
Thanks, this seems to be just what I wanted!
Jacob Bellamy