我可以通过搜索Active Directoy使用C#检索的域名和用户名可以通过、用户名、域名、Directoy

2023-09-08 13:30:56 作者:AV丶屌丝女优

所有,

我有用户的电子邮件的大名单,我需要获得用户名和域名为他们每个人。

I have a big list of user emails, and I need to get the username and domain name for each one of them.

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

My organization contains lots of domains and our users log on to their machine using usernames that are different from their email addresses.

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

Please advise if we can write a C# utility that can search AD using the email of each user, or if we can do it in a simpler way.

推荐答案

您在.NET 3.5?如果是这样的 - 广告在.NET 3.5中强大的新功能 - 看看这篇文章管理目录安全主体在.NET 3.5 通过伊森Wilanski和乔·卡普兰。

Are you on .NET 3.5 ? If so - AD has great new features in .NET 3.5 - check out this article Managing Directory Security Principals in .NET 3.5 by Ethan Wilanski and Joe Kaplan.

其中一个重大的新特性是PrincipalSearcher级这应会大大简化了查找用户和/或组的AD。

One of the big new features is a "PrincipalSearcher" class which should greatly simplify finding users and/or groups in AD.

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

If you cannot use .NET 3.5, use a DirectorySearcher and specify the e-mail address as your search criteria, and retrieve the user name (which one? There's a gazillion different usernames!):

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
}

希望这有助于!

Hope this helps!

马克·