有没有办法区分“人员用户账户”和“计算机用户账户”?

6

在查询Active Directory的用户时,是否有一种方法可以过滤掉为计算机创建的用户帐户?最好是一种在大多数典型网络中通用的方法。例如:

DirectorySearcher ds = new DirectorySearcher(new DirectoryEntry([Users_OU_root]));    
ds.filter = "(&(objectClass=User)([CRITERIA_TO_FILTER_OUT_COMPUTER_USER_ACCOUNTS]))";    
ds.FindAll();    
...
2个回答

6
如果您使用的是 .NET 3.5 或更高版本,则应该查看 System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。在此处阅读有关它的所有信息: 基本上,您可以定义一个域上下文并轻松地在 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都很有益,因为它被索引,并且不是多值的-因此查询性能会更好。
对于真实用户,请在LDAP过滤器中使用objectCategory = Person,而对于计算机,请使用objectCategory = Computer

4

即使我同意答案,Active Directory 仍然是一个 LDAP 服务器。这是您要查找的过滤器:

(&(objectCategory=user)(objectClass=user)(...))

'objectCategory=user'是一个快捷方式,用于表示'objectCategory=CN=User,CN=Schema,CN=Configuration,DC=dom,DC=fr',它被Active-Directory理解,但也可以在其他目录中使用,这就是我提供答案的原因,即使已经有另一个答案被接受。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接