2009-07-14 62 views
0

所有,我可以使用C#

我有用户的电子邮件的大名单中搜索Active Directoy检索域名和用户名,我需要为他们每个人得到的用户名和域名。

我的组织含有大量的域和我们的用户登录到其使用机器的用户名是从他们的电子邮件地址不同。

请告知,如果我们可以写一个C#程序,可以使用每个用户的电子邮件搜索广告,或者如果我们能以更简单的方式做到这一点。

回答

1

你在.NET 3.5上吗?如果是这样的 - 广告在.NET 3.5强大的新功能 - 看看这篇文章Managing Directory Security Principals in .NET 3.5由伊森Wilanski和乔·卡普兰。

其中一个大的新功能是“PrincipalSearcher”级应该大大简化了查找用户和/或组中的AD。

如果您不能使用.NET 3.5,使用的DirectorySearcher并指定电子邮件地址作为搜索条件,并获取用户名(其中一个有一个极大不同的用户名?!):

DirectoryEntry deRoot = new DirectoryEntry("LDAP://cn=Users,dc=yourdomain,dc=com"); 

DirectorySearcher deSrch = new DirectorySearcher(deRoot); 

deSrch.SearchScope = SearchScope.Subtree; 

deSrch.PropertiesToLoad.Add("sn"); // surname = family name 
deSrch.PropertiesToLoad.Add("givenName"); 
deSrch.PropertiesToLoad.Add("samAccountName"); 

deSrch.Filter = string.Format("(&(objectCategory=person)(mail={0}))", emailAddress); 

foreach(SearchResult sr in deSrch.FindAll()) 
{ 
    // you can access the properties of the search result 
    if(sr.Properties["sn"] != null) 
    { 
    string surname = sr.Properties["sn"][0].ToString(); 
    } 
    // and so on, for all the other properties, too 
} 

希望这有助于!

马克

1

如果该数据是所有AD的话,你也许可以使用LDAP查询。在这种情况下,因为您使用的是.NET,所以我建议您使用DirectorySearcher