Search code examples
c#sslldapoktadirectoryservices

Connecting or Accessing Okta LDAP Interface using c# .NET Client


I have a client which uses Okta LDAP Interface facility. We have a LDAP v3 tool which connects with AD, Open LDAP other LDAP v3 supported servers.

We want to integrate Okta LDAP Interface into our tool as it is LDAPv3 Compatible. Our Code is based on .NET framework + C Sharp.

We are facing some issues/challenges while connecting with Okta LDAP Interface.

We use System.DirectoryServices by Microsoft library provided by microsoft currently. But facing issues with LDAP Interface.

For StartTLS/389

I get the error :

Unwilling to perform. LDAP Error Code 53

More: A secure connection cannot be established. To admin: This service requires TLS. LDAP

For SSL/636

Error: The server is not operational.

Links:

https://learn.microsoft.com/en-us/dotnet/api/system.directoryservices?view=netframework-4.8

https://learn.microsoft.com/en-us/dotnet/api/system.directoryservices.directoryentry?view=netframework-4.8

https://ldapwiki.com/wiki/LDAP_UNWILLING_TO_PERFORM

var oktaLDAPPath = "LDAP://dev-506668.ldap.oktapreview.com:636/ou=users,dc=dev-506668,dc=oktapreview,dc=com";
        var un = "uid=*******,dc=dev-506668,dc=oktapreview,dc=com";
        var pass = "*******";
        var filter = "((objectClass=*))";
        try
        {
            using (var userDirectoryEntry = new DirectoryEntry(oktaLDAPPath, un, pass,AuthenticationTypes.SecureSocketsLayer))
            { 
                using (var directorySearcher = new DirectorySearcher(userDirectoryEntry, filter) { PageSize = 100 })
                {
                    directorySearcher.FindOne();
                }
            }
        }
        catch (DirectoryServicesCOMException dex)
        {

        }
        catch (Exception ex)
        {

        }

Thanks


Solution

  • Update: So I did some testing for myself. I see what's going on.

    If you do a DNS lookup on dev-506668.ldap.oktapreview.com, it gives you a CNAME result to op1-ldapi-fb96b0a1937080bd.elb.us-east-1.amazonaws.com.

    A browser will use the IP address of the CNAME, but still make the request with the host name that you originally gave it. However, for some reason, when starting an LDAP connection, Windows is using the CNAME to iniatate the connection.

    In other words, Windows is changing the request to LDAP://op1-ldapi-fb96b0a1937080bd.elb.us-east-1.amazonaws.com:636. But then it receives the SSL certificate which has the name *.ldap.oktapreview.com and it panics because that doesn't match the name it used to make the request (op1-ldapi-fb96b0a1937080bd.elb.us-east-1.amazonaws.com).

    I verified all of this using Wireshark, monitoring traffic on port 636. The SSL Client Hello is using op1-ldapi-fb96b0a1937080bd.elb.us-east-1.amazonaws.com instead of dev-506668.ldap.oktapreview.com.

    I don't know of a way to make it not do that. DirectoryEntry has no way to override how it verifies the SSL certificate either. LdapConnection does, which you can see here, but it might be a little harder to work with. I've never used it. (you probably should do some verification of your own and no just return true like that example does).

    This might be something you can share with Okta Support anyway.


    Original answer:

    It sounds like your computer does not trust the SSL certificate that is used on the server. To verify this, I use Chrome. You have to start Chrome like this:

    chrome.exe --explicitly-allowed-ports=636
    

    Then you can put this in the address bar:

    https://dev-506668.ldap.oktapreview.com:636
    

    If the certificate is not trusted, you will get a big error saying so. You can click the 'Advanced' button to see the reason Chrome gives for it not being trusted. But Chrome will also let you inspect the certificate by clicking on "Not secure" in the address bar to the left of the address, then click 'Certificate'.

    There a couple reasons it might not be trusted:

    1. The fully-qualified domain name you are using (dev-506668.ldap.oktapreview.com) doesn't match what is on the certificate. If that's the case, you might be able to just change the domain name you use to match the certificate.
    2. The certificate is not issued by a trusted authority. It could be a self-signed certificate. If this is the case, then you should see an "Install Certificate" button when you view the certificate, which you can use to explicitly trust the certificate. See here for screenshots, starting on step 3. This will only apply to the current computer.