有什么方法可以区分"人员用户帐户"和"计算机用户帐户"吗?

Ric*_*ich 6 .net c# active-directory

在为用户查询Active Directory时 - 有没有办法过滤掉为计算机创建的用户帐户?理想情况下,这种方式在大多数典型网络中都很常见.例如:

DirectorySearcher ds = new DirectorySearcher(new DirectoryEntry([Users_OU_root]));    
ds.filter = "(&(objectClass=User)([CRITERIA_TO_FILTER_OUT_COMPUTER_USER_ACCOUNTS]))";    
ds.FindAll();    
...
Run Code Online (Sandbox Code Playgroud)

mar*_*c_s 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
   }
}
Run Code Online (Sandbox Code Playgroud)

新的S.DS.AM使得在AD中使用用户和群组变得非常容易:

计算机帐户将显示为ComputerPrincipal(派生自Principal) - 因此您可以轻松地将用户和计算机帐户分开.

如果您不能或不想转移到S.DS.AM - 您还可以使用objectCategoryLDAP过滤器中的objectClass代替用户和计算机.objectCategory无论如何,它是有益的,因为它是索引的,而不是多值的 - 所以查询性能会好得多.

对于真实用户,objectCategory = Person在计算机中使用objectCategory = ComputerLDAP过滤器.