【问题标题】:Searching Active Directory Users Only in Specified Group in C#仅在 C# 中的指定组中搜索 Active Directory 用户
【发布时间】:2013-02-07 22:55:27
【问题描述】:

我一直在努力寻找一种查询指定 AD 组成员的好方法。

我在查找组,甚至根据条件查询用户方面都没有问题。

目前我有

 PrincipalContext context = new PrincipalContext(ContextType.Domain, _domain, ADServerUser, ADServerPassword);
 UserPrincipal userPrinciple = new UserPrincipal(context);
 userPrinciple.GivenName = "stringToSearchForFirstName";
 userPrinciple.Name = "stringToSearchForUserName";
 userPrinciple.Surname = "stringToSearchForLastName";
 PrincipalSearcher srch = new PrincipalSearcher(new UserPrincipal(context));                    
 srch.QueryFilter = userPrinciple;
 var result = srch.FindAll();

这给了我我想要的所有用户,但它不会过滤组。

我可以将 GroupPrincipal 对象与主体搜索一起使用,但我无法过滤掉用户。

我有点想要一种方法,能够同时应用 UserPrincipal 和 GroupPrincipal 来过滤由 Group 和 User 参数返回的结果。

我使用 linq where 子句尝试进行匹配以查看用户是否在组中,但是当我获取所有用户时,查询超时。总体来说很有意义。

但是,如果我查询该组,我无法使用 principalSearcher 来应用查询。

关于如何做到这一点的任何想法?

【问题讨论】:

  • Blast_dan,请说明这段代码所在的方法。很难确定你是如何定义pFirstName, pUserName, pLastName and context
  • 它们是简单的方法属性。字符串。
  • 我看到你的一个问题 Blast_dan,你在哪里通过组搜索..?
  • 就是这个问题,不知道怎么传进群里搜索。

标签: c# .net active-directory


【解决方案1】:
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, _domain);
// get the AD Group you are wanting to Query
GroupPrincipal group = GroupPrincipal.FindByIdentity("cn=YourGroupname");
foreach(Principal p in group.Members)
{
    //do what ever coding you need to do here            
}

【讨论】:

  • 对不起,我添加了上下文,我也不关心组名,我想在指定组中搜索并将过滤器应用于指定组中的用户。然后我应该返回用户
  • 不,我想过滤成员的名字和姓氏。我找不到同时查询用户和组的方法
【解决方案2】:

根据我的研究,我得出的结论是,使用主体对象来过滤组和用户参数是不可能的。我们需要恢复使用查询字符串方法来解决问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多