【问题标题】:Any way to distinguish between "people user accounts" and "computer user accounts"?有什么方法可以区分“人员用户帐户”和“计算机用户帐户”吗?
【发布时间】:2011-06-24 09:53:42
【问题描述】:

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

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

【问题讨论】:

    标签: c# .net active-directory


    【解决方案1】:

    如果您使用的是 .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 - 您还可以通过使用 objectCategory 而不是 LDAP 过滤器中的 objectClass 将用户和计算机分开。 objectCategory 无论如何都是有益的,因为它是索引的,而不是多值的 - 所以查询性能会好得多。

    对于真实用户,请使用 objectCategory = Person,而对于计算机,请在 LDAP 过滤器中使用 objectCategory = Computer

    【讨论】:

      【解决方案2】:

      即使我同意这个答案。 Active-Directory 仍然是一个 LDAP 服务器。这是您正在寻找的过滤器:

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

      'objectCategory=user' 是 Active-Directory 理解的 'objectCategory=CN=User,CN=Schema,CN=Configuration,DC=dom,DC=fr' 的快捷方式,但它也是其他目录中的一种方式,这就是我提出答案的原因,即使接受了另一个答案。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多