DirectoryEntry.Exists and "special" chars in DN

1
DirectoryEntry groupEntry = new DirectoryEntry("LDAP://CN=Sales,CN=Users,DC=contoso,DC=com");
List<string> list = new List<string>();
foreach(string dn in groupEntry.Properties["members"]) {
    try {
        if(DirectoryEntry.Exists("LDAP://"+dn)) {
            list.Add(dn);
        }
    } catch(Exception e) {
        list.Add(e.Message);
    }
}
return list;

The list returned should be

CN=Sales Americas,CN=Users,DC=contoso,DC=com
CN=Sales EMEA,CN=Users,DC=contoso,DC=com
CN=Sales D/A/CH,CN=Users,DC=contoso,DC=com
CN=Sales SEA,CN=Users,DC=contoso,DC=com

but it is

CN=Sales Americas,CN=Users,DC=contoso,DC=com
CN=Sales EMEA,CN=Users,DC=contoso,DC=com
Unbekannter Fehler (0x80005000)
CN=Sales SEA,CN=Users,DC=contoso,DC=com

I think that the problem is the / special character. How would I have to encode this character for it to work in DirectoryEntry.Exists? Do you know other special characters that can be part of a CN name, which I have to encode?

c#
active-directory
asked on Stack Overflow Jan 14, 2015 by Alexander

1 Answer

1

The values in members attribute are already valid DN.
But when putting DN into LDAP path you still have to escape the "/", which don't need escape in DN but not in LDAP path.

You can simply replace any "/" with "\/" in DN.

answered on Stack Overflow Jan 15, 2015 by baldpate

User contributions licensed under CC BY-SA 3.0