LDAP DirectorySearcher with MemberOf property

I want to find all the users who are members of a group in a specific organizational unit, so my filter will look something like this:

(& (object = user) (memberOf = * OU = something, OU = yes, DC = DEV, DC = local))

Is there a way to run directorysearcher in a memberof property with a wildcard?

+3
source share
5 answers

You need to set the OU that you want to look up as the root of your DirectorySearcher:

DirectoryEntry myOU = new DirectoryEntry("OU=something,OU=yep,DC=dev,DC=local");
DirectorySearcher srch = new DirectorySearcher(myOU);
srch.SearchScope = SearchScope.Subtree;

objectCategory = person . objectCategory, , , , objectClass ( ):

srch.Filter = "(objectCategory=person)";

, OU, :

srch.Filter = "(&(objectCategory=person)(memberOf=cn=Group,ou=yep,dc=dev,dc=local))";

- , LDAP , RDN, DN .

+8

, DN Active Directory.

+2

memberOf.

0

memberOf. "(objectClass=user)"

0

This is how I did it. The LDAP name is the group for which you need members.

DirectoryEntry entry = new DirectoryEntry("LDAP://<COMPANYLDAP>/CN=<Group Name>,OU=something,OU=yep,DC=dev,DC=local");
DirectorySearcher Dsearch = new DirectorySearcher(entry);
SearchResult sResultSet = Dsearch.FindOne();
GetProperty(sResultSet, "member");



 public static void GetProperty(SearchResult searchResult, string PropertyName)
        {
            StringBuilder strb = new StringBuilder();
            if (searchResult.Properties.Contains(PropertyName))
            {

                ResultPropertyValueCollection rc = searchResult.Properties[PropertyName];
                foreach (string name in rc)
                {
                    DirectoryEntry entry = new DirectoryEntry("LDAP://<COMPANYLDAP>/" + name);
                    DirectorySearcher Dsearch = new DirectorySearcher(entry);
                    //Dsearch.Filter = name;
                    SearchResult sResultSet = Dsearch.FindOne();
                    strb.AppendLine(GetPropertyvalue(sResultSet, "displayname") + "," + GetPropertyvalue(sResultSet, "mail"));
                }


            }

            File.WriteAllText(strb.ToString(), "c:\\Users.txt");
        }
0
source

Source: https://habr.com/ru/post/1719121/


All Articles