简体   繁体   中英

C# - Find all email addresses for an Active Directory user

I'm trying to get all the email addresses associated to a given AD user.

For the user I have the domain and the login name (ex. DOMAIN\\UserName) and I the AD is storing the email addresses in:

  1. The mail attribute.
  2. In proxyAddresses attributes.

So far, I don't know what C# API to use to connect to the AD, and how to properly filter by the user to fetch all the email addresses. I'm using .NET 3.5.

Thank you.

Here's a possible solution using various classes in the System.DirectoryServices namespace.

string username = "username";
string domain = "domain";

List<string> emailAddresses = new List<string>();

PrincipalContext domainContext = new PrincipalContext(ContextType.Domain, domain);
UserPrincipal user = UserPrincipal.FindByIdentity(domainContext, username);

// Add the "mail" entry
emailAddresses.Add(user.EmailAddress);

// Add the "proxyaddresses" entries.
PropertyCollection properties = ((DirectoryEntry)user.GetUnderlyingObject()).Properties;
foreach (object property in properties["proxyaddresses"])
{
   emailAddresses.Add(property.ToString());
}

Have you looked at the DirectoryEntry class. You can pull properties from there given you have the LDAP string set up. The propery for mail is "mail" ironic aint it ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM