我可以通过使用C#搜索Active Directoy来检索域名和用户名吗?

时间:2022-01-08 05:47:15

All,

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.

我的组织包含许多域,我们的用户使用与其电子邮件地址不同的用户名登录到他们的计算机。

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.

请告知我们是否可以编写可以使用每个用户的电子邮件搜索AD的C#实用程序,或者我们是否可以以更简单的方式执行此操作。

2 个解决方案

#1


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.

你是.NET 3.5吗?如果是这样 - AD在.NET 3.5中有很多新功能 - 请参阅本文由Ethan Wilanski和Joe Kaplan管理.NET 3.5中的目录安全主体。

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

其中一个重要的新功能是“PrincipalSearcher”类,它应该极大地简化在AD中查找用户和/或组的过程。

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!):

如果您不能使用.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
}

Hope this helps!

希望这可以帮助!

Marc

#2


If that data is all in AD, then you can probably query it using LDAP. In which case, I would recommend DirectorySearcher since you are using .NET.

如果该数据全部在AD中,那么您可以使用LDAP查询它。在这种情况下,我会推荐DirectorySearcher,因为您使用的是.NET。

#1


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.

你是.NET 3.5吗?如果是这样 - AD在.NET 3.5中有很多新功能 - 请参阅本文由Ethan Wilanski和Joe Kaplan管理.NET 3.5中的目录安全主体。

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

其中一个重要的新功能是“PrincipalSearcher”类,它应该极大地简化在AD中查找用户和/或组的过程。

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!):

如果您不能使用.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
}

Hope this helps!

希望这可以帮助!

Marc

#2


If that data is all in AD, then you can probably query it using LDAP. In which case, I would recommend DirectorySearcher since you are using .NET.

如果该数据全部在AD中,那么您可以使用LDAP查询它。在这种情况下,我会推荐DirectorySearcher,因为您使用的是.NET。