使用Active Directory c#从组中获取所有用户的电子邮件

时间:2012-10-04 14:14:34

标签: c#

我需要特定群组的所有电子邮件地址。请帮帮我

string filter1 = string.Format("(&(objectClass=group)(cn={0}))", "groupname");
        DirectorySearcher searcher = new DirectorySearcher(entry);
        searcher.Filter = filter1;
        searcher.SearchScope = SearchScope.Subtree;
        searcher.PropertiesToLoad.Add("member");
        SearchResult res = searcher.FindOne();
        ArrayList userNames = new ArrayList();
        if (res != null)
        {
            for (int counter = 0; counter <res.Properties["member"].Count; counter++)
            {
                string user = (string)res.Properties["member"][counter];
                userNames.Add(user);
            }
        }

我正在获取姓名和其他详细信息,但没有收到电子邮件。请告诉我直接查找每个用户的电子邮件地址的方法。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用此代码 - 基于PrincipalContext

var username = "username";
var domain = "domain";
var emailAddresses = new List<string>();

var principalContext = new PrincipalContext(ContextType.Domain, domain);
var userPrincipal  = UserPrincipal.FindByIdentity(principalContext, username);
// Add the "mail" entry
emailAddresses.Add(userPrincipal.EmailAddress);

链接:http://msdn.microsoft.com/fr-fr/library/bb344891(v=vs.90).aspx