c# - 有什么方法可以区分 "people user accounts"和 "computer user accounts"吗?

标签 c# .net 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();    
...

最佳答案

如果您使用的是 .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 无论如何都是有益的,因为它是索引的,而不是多值的 - 因此查询性能会好得多。

对于现实生活中的用户,使用 objectCategory = Person,而对于计算机,在 LDAP 过滤器中使用 objectCategory = Computer

关于c# - 有什么方法可以区分 "people user accounts"和 "computer user accounts"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6466189/

相关文章:

c# - DataGridView 只允许单击 F2 进行编辑

c# - ASP.NET MVC 获取具有特定属性的数据库条目列表

c# - 如何将 wchar_t* 从 C++ 编码到 C# 作为输出参数或返回值?

Powershell - Get-AdGroupMember 超出限制

azure - 如何使用 Azure AD 的 MSAL 库仅允许登录客户端应用程序中的某些角色

c# - 将多维数组转换为单个数组列表

c# - 用于组织的 Linq 表达式(类似于 Group By 但不同)

c# - 没有指定authenticationScheme,也没有找到默认认证和自定义授权的DefaultChallengeScheme

.net - CLR识别的线程

apache - 如何通过外部托管站点强制执行 Azure AD 登录?