Set callback for System.DirectoryServices.DirectoryEntry to handle self-signed SSL certificate?

后端 未结 3 1430
谎友^
谎友^ 2020-12-01 22:26

I have an application replicating data from a directory service using typical System.DirectoryServices.DirectoryEntry code. I now have a requirement to replicate from Novell

3条回答
  •  無奈伤痛
    2020-12-01 22:58

    This is a phenomenal question.

    I've been battling this same issue for a few days now, and I've finally got some definitive proof on why the DirectoryEntry object will not work in this scenario.

    This particular Ldap server (running on LDAPS 636) also issues it's own self signed certificate. Using LdapConnection (and monitoring the traffic via Wireshark), I noticed a handshake taking place that does not occur when using DirectoryEntry :

    enter image description here

    The first sequence is the from the secured ldap server, the second sequence is from my machine. The code that prompts the second sequence is :

    ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
    

    There are others way to "fake out" the callback, but this what I've been using.

    Unfortunately, DirectoryEntry does not have an option or method to verify a self signed cert, thus the acceptance of the certificate never happens (second sequence), and the connection fails to initialize.

    The only feasible way to accomplish this is by using LdapConnection, in conjunction with a SearchRequest and SearchResponse. This is what I've got so far :

    LdapConnection ldapConnection = new LdapConnection("xxx.xxx.xxx:636");
    
    var networkCredential = new NetworkCredential("Hey", "There", "Guy");
    ldapConnection.SessionOptions.SecureSocketLayer = true;
    ldapConnection.SessionOptions.VerifyServerCertificate += delegate { return true; };
    ldapConnection.AuthType = AuthType.Negotiate;
    ldapConnection.Bind(networkCredential);
    
    SearchRequest request = new SearchRequest("DC=xxx,DC=xxx,DC=xxx", "(sAMAccountName=3074861)", SearchScope.Subtree);
    SearchResponse response = (SearchResponse)ldapConnection.SendRequest(request);
    
    if(response.Entries.Count == 1)
    {SearchResultEntry entry = response.Entries[0];
     string DN = entry.DistinguishedName;}
    

    From there you can gather AD Properties from the SearchResponse, and process accordingly. This is a total bummer though, because the SearchRequest seems to be much slower then using the DirectoryEntry.

    Hope this helps!

提交回复
热议问题