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

C# 使用Active Directory c从组中获取所有用户的电子邮件#,c#,C#,我需要特定组的所有电子邮件地址。请帮帮我 string filter1 = string.Format("(&(objectClass=group)(cn={0}))", "groupname"); DirectorySearcher searcher = new DirectorySearcher(entry); searcher.Filter = filter1; searcher.SearchScope = SearchScope.S

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

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);
            }
        }
stringfilter1=string.Format((&(objectClass=group)(cn={0})),“groupname”);
DirectorySearcher search=新的DirectorySearcher(条目);
searcher.Filter=filter1;
searcher.SearchScope=SearchScope.Subtree;
searcher.PropertiesToLoad.Add(“成员”);
SearchResult res=searcher.FindOne();
ArrayList用户名=新的ArrayList();
如果(res!=null)
{

对于(int counter=0;counter您可以尝试使用此代码-基于
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);
var username=“username”;
var domain=“domain”;
var emailAddresses=新列表();
var principalContext=新的principalContext(ContextType.Domain,Domain);
var userPrincipal=userPrincipal.FindByIdentity(principalContext,用户名);
//添加“邮件”条目
emailAddresses.Add(userPrincipal.EmailAddress);

链接:

谢谢您的回复,但我使用的是.net 2.0版。我将编辑我的问题以获得更清晰的信息。我很乐意帮助您,PrincipalContext存在于2.0版上,示例:我尝试了您的示例,但PrincipalContext未被.net识别。我没有任何添加名称空间的选项。添加assemly之前添加名称空间我已经添加了,没有system.DirectoryServices程序集,但.net 2.0没有Using system.DirectoryServices.AccountManagement命名空间