在为用户查询 Active Directory 时 - 有没有办法过滤掉为计算机创建的用户帐户?理想情况下,这是一种在大多数典型网络中都很常见的方式。例如:
DirectorySearcher ds = new DirectorySearcher(new DirectoryEntry([Users_OU_root]));
ds.filter = "(&(objectClass=User)([CRITERIA_TO_FILTER_OUT_COMPUTER_USER_ACCOUNTS]))";
ds.FindAll();
...
最佳答案
如果您使用的是 .NET 3.5 及更高版本,则应查看 System.DirectoryServices.AccountManagement
(S.DS.AM) 命名空间。在这里阅读所有相关信息:
- Managing Directory Security Principals in the .NET Framework 3.5
- MSDN docs on System.DirectoryServices.AccountManagement
基本上,您可以定义域上下文并在 AD 中轻松找到用户和/或组:
// set up domain context
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// do something here....
}
// find the group in question
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, "YourGroupNameHere");
// if found....
if (group != null)
{
// iterate over members
foreach (Principal p in group.GetMembers())
{
Console.WriteLine("{0}: {1}", p.StructuralObjectClass, p.DisplayName);
// do whatever you need to do to those members
}
}
新的 S.DS.AM 使得在 AD 中与用户和组一起玩变得非常容易:
计算机帐户将显示为 ComputerPrincipal
(派生自 Principal
)- 因此您可以轻松地将用户和计算机帐户分开。
如果您不能或不想迁移到 S.DS.AM - 您还可以通过在 LDAP 过滤器中使用 objectCategory
而不是 objectClass 来将用户和计算机分开。 objectCategory
无论如何都是有益的,因为它是索引的,而不是多值的 - 因此查询性能会好得多。
对于现实生活中的用户,使用 objectCategory = Person
,而对于计算机,在 LDAP 过滤器中使用 objectCategory = Computer
。
关于c# - 有什么方法可以区分 "people user accounts"和 "computer user accounts"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6466189/