【问题标题】:Finding all groups that a user manages查找用户管理的所有组
【发布时间】:2012-08-21 12:33:39
【问题描述】:

我们得到了一个特殊的多值属性。我们称之为ourOwnManagedBy,它可以包含管理当前组的用户或组(他们的DN)。

如何检索特定用户管理的所有组的列表(在managedByourOwnManagedBy 的帮助下)?

例如。假设用户是组 GlobalAdministrators 的成员,并且组 ApplicationAdministrators 的成员是 GlobalAdministrations。最后是在 ourOwnManagedBy 属性中具有 ApplicationAdministrators 的组 MyApplication。

  • UserGlobalAdministrators 的成员
  • GlobalAdministratorsApplicationAdministrators 的成员
  • MyApplicationourOwnManagedBy 中获得了ApplicationAdministrators

如何使用该信息来查找特定用户管理的所有组?是否可以在自定义属性(包含用户和组的 DN)中进行某种递归检查?

更新

我尝试过使用这样的目录搜索过滤器:

string.Format("(ourOwnManagedBy:1.2.840.113556.1.4.1941:={0})", dn);

但我可能误解了1.2.840.113556.1.4.1941 的作用? (MSDN page)

【问题讨论】:

  • 这很愚蠢,但链接站点上的代码示例将其 DN 括在大括号中。你有同样的尝试吗?我不认为它会有所作为,但我还没有尝试过。
  • 是的,试过了。不过谢谢你的建议

标签: c# active-directory ldap


【解决方案1】:

恐怕仅靠一个 LDAP 查询是不可能完成的。您必须将其拆分为子查询并分别运行每个子查询,如果有很多要迭代的内容,这反过来会阻塞域控制器。

我尝试按照我描述的方式进行操作,但性能非常糟糕,至少使用 .NET 的可用模块来实现。

【讨论】:

    【解决方案2】:

    以下页面显示3.1.1.3.4.4 LDAP Matching Rules (extensibleMatch) 表示您正在使用的 LDAP_MATCHING_RULE_TRANSITIVE_EVAL 在 Windows 2008 和更高版本中工作。如果您使用的是 2003,它可能无法正常工作。

    【讨论】:

    • 这如何回答我的问题?
    • 您没有提及您使用的是哪个操作系统。 Windows 2003 不会出错,它不会只返回任何结果。
    【解决方案3】:

    没有递归,不知道它将如何提高性能,可能有错误。

            string user = "username";
            //get domain
            DirectoryEntry de = System.DirectoryServices.ActiveDirectory.Domain.GetCurrentDomain().GetDirectoryEntry();
            //get users dn first
            string userDN;
            using (var searcher = new DirectorySearcher(de))
            {
                searcher.Filter = String.Format("(&(objectCategory=person)(objectClass=user)(sAMAccountName={0}))", user);
                searcher.PropertiesToLoad.Add("distinguishedName");
                userDN = searcher.FindOne().Properties["distinguishedName"][0].ToString();
            }
    
            //get list of all users groups
            List<string> groups;
            //see http://stackoverflow.com/questions/6252819/find-recursive-group-membership-active-directory-using-c-sharp
            using (var searcher2 = new DirectorySearcher(de))
            {
                searcher2.Filter = String.Format("(member:1.2.840.113556.1.4.1941:={0})", userDN);
                searcher2.SearchScope = SearchScope.Subtree;
                searcher2.PropertiesToLoad.Add("distinguishedName");
    
                SearchResultCollection src = searcher2.FindAll();
    
                groups = (from SearchResult c in src
                          select c.Properties["distinguishedName"][0].ToString()).ToList();
            }
    
            //build giant search query
            SearchResultCollection srcGroups;
            using (var searcher = new DirectorySearcher(de))
            {
                string baseString = "(|{0})";
                string managedbybase = "(managedBy={0})";
                //I've read that you can search multivalued lists using a standard ='s.
                string ourOwnManagedByBase = "(ourOwnManagedBy={0})";
    
                StringBuilder sb = new StringBuilder();
    
                //add user DN to list of group dn's
                groups.Add(userDN);
    
                foreach (string g in groups)
                {
                    sb.AppendFormat(managedbybase, g);
                    sb.AppendFormat(ourOwnManagedByBase, g);
                }
    
                searcher.Filter = string.Format(baseString, sb.ToString());
                srcGroups = searcher.FindAll();
            }
    

    老实说,这实际上对我不起作用 :) 但我认为这是因为我们的域的配置方式。如果不出意外,它可能会将您推向正确的方向。

    【讨论】:

    猜你喜欢
    • 2016-09-15
    • 1970-01-01
    • 2021-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-29
    • 2021-09-07
    • 1970-01-01
    相关资源
    最近更新 更多