2012-10-04 71 views
0

我需要特定组的所有电子邮件地址。请帮我使用Active Directory获取组中的所有用户电子邮件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); 
      } 
     } 

我得到uesr名称和其他细节,但没有得到电子邮件。请告诉我直接找到每个用户的电子邮件地址的方法。

+2

[?**你尝试过什么自己**](http://www.whathaveyoutried.com ) –

+1

http://bit.ly/QJw5IJ –

回答

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

+0

感谢您的回复,但我使用.net版本2.0。我会编辑我的问题更清晰 – user1685652

+0

我很乐意为您提供帮助,PrincipalContext存在于2.0,示例:http://www.codeproject.com/Articles/38344/Using-System-DirectoryServices-AccountManagement –

+0

我试过你的例子但是PrincipalContext不能被.net识别。我没有任何添加任何名称空间的选项。 – user1685652

相关问题