简体   繁体   中英

DirectoryEntry.Exists and “special” chars in DN

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM