如何获取“专有名称”列表使用C#从活动目录中的所有安全组 [英] How do I get the list of "distinguished name" of all security groups from active directory using C#

查看:107
本文介绍了如何获取“专有名称”列表使用C#从活动目录中的所有安全组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





这里的任何人都知道如何使用c#代码从Active Directory获取组织单位(OU)中安全组的专有名称列表?



我使用了下面的代码,它只给出了活动目录中所有OU的专有名称列表。但我想要的是获得所有安全组的专有名称列表。



我尝试了什么:



Hi,

Anyone here know how to get the list of "distinguished name" of security groups in an Organisation Unit (OU) from Active directory using c# code?

I have used the below code which will give only the list of "distinguished name" of all the OUs from active directory. But what I want is to get the list of "distinguished name" of all the SECURITY GROUP INSIDE THAT OU.

What I have tried:

public static List<string> ListOu()
    {
        List<string> ous = new List<string>();
        using (DirectoryEntry root = new DirectoryEntry("LDAP://dc=DOMAIN,dc=COM"))
        {
            DirectorySearcher searcher = new DirectorySearcher(root);
            searcher.Filter = "(&(objectClass=organizationalUnit))";
            searcher.SearchScope = SearchScope.Subtree;
            searcher.PropertiesToLoad.Add("distinguishedName");

            var result = searcher.FindAll();
            foreach (SearchResult entry in result)
            {
                ous.Add(entry.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString());
            }        

            result.Dispose();
            searcher.Dispose();
        }
        return ous;
    }

推荐答案

安全组的专有名称包括组织单位,因此不是第一个获得单位地方,只查询所有群组



The distinguished name for a Security Group includes the Organisational Unit, therefore instead of getting the Units in the first place, just query for all Groups

public static List<string> ListOu()
    {
        List<string> ous = new List<string>();
        using (DirectoryEntry root = new DirectoryEntry("LDAP://dc=DOMAIN,dc=COM"))
        {
            DirectorySearcher searcher = new DirectorySearcher(root);
            // Get all Groups
            searcher.Filter = "(&(objectClass=group))";
            searcher.SearchScope = SearchScope.Subtree;
            searcher.PropertiesToLoad.Add("distinguishedName");

            SearchResultCollection result = searcher.FindAll();
            foreach (SearchResult entry in result)
            {
        ous.Add(entry.GetDirectoryEntry().Properties["distinguishedName"].Value.ToString());                           
            }        

            result.Dispose();
            searcher.Dispose();
        }
        return ous;
    }





亲切的问候



Kind Regards


这篇关于如何获取“专有名称”列表使用C#从活动目录中的所有安全组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆