views:

89

answers:

1

I want to search for a user's email by using Active Directory. Available is the user's full name (ex. "John Doe" for the email with an email "[email protected]"). From what I've searched, this comes close to what I'm looking to do -- except that the Filter is set to "SAMAccountName", which is not what I have.

Unless I'm misunderstanding, I just need to pick the right attribute and toss in the full name name in the same way. Unfortunately, I don't know what this attribute is, apparently no one has had to ask about searching for information in this manner, and that is a pretty big list (msdn * microsoft * com/en-us/library/ms675090(v=VS.85) * aspx, stackoverflow didn't let me link 2 hyperlinks because I don't have 10 rep) of attributes.

Does anyone know how to obtain the user's email address through an Active Directory lookup by using the user's full name?

+2  A: 

Using the 3.5 System.DirectoryServices.AccountManagement.dll, you can use the UserPrincipal FindByIdentity method:

UserPrincipal.FindByIdentity(context, IdentityType.Name, "Lastname, Firstname");

This call is generally wrapped inside a couple of using statements like the following:

using (var ctx = new PrincipalContext(ContextType.Domain))
{     
    using (var userPrincipal = UserPrincipal.FindByIdentity(ctx, IdentityType.Name, user))
    {
        return userPrincipal == null ? "" : userPrincipal.EmailAddress;
    }
 }
Bermo