How to use UTF-8 encoding in .NET LDAP queries

If you are using System.DirectoryServices.Protocols in .NET to query LDAP directories, you may encounter strange replacement characters like � in your results containing special characters, especially when deploying on Linux environments.

This is due to the default LDAP protocol version is 2. Microsoft added support for UTF-8 encoded string attributes in LDAP version 3 (see here).

An easy fix to this is setting the protocol version for LdapConnection to 3 like here:

//omitted for brevity
var connection = new LdapConnection(ldapDirectoryIdentifier, credentials)
{
    SessionOptions = { ProtocolVersion = 3 }
}

After that you will get correctly encoded LDAP attributes which contain special latin characters like german umlauts.


I hope this little guide helped you. Happy coding 🥳