How to connect AD with LDAPS protocol with DirectoryContext or NtdsApi?

FelixChen 21 Reputation points
2020-09-18T12:13:45.91+00:00

According to following article, setup a LADPS environment:
Step by Step Guide to Setup LDAPS on Windows Server

After that, I hope to connect the forest by LDAPS protocol, but I found that the .Net class “DirectoryContext” doesn’t provide any parameter about port. Also, cannot set the port by name too. (like “corp.fabrikam.com:636”)

I check the NtdsAPI too, looks that it doesn’t support LDAPS too. For example:

As far as I know, .Net class DirectoryEntry support LDAPS, but for some reason, I have to use DirectoryContext or NtdsAPI...

Any help or suggestion is appreciated.
Thank you!

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,498 questions
{count} votes

Accepted answer
  1. Rita Han - MSFT 2,161 Reputation points
    2020-09-21T07:23:11.183+00:00

    Hello @FelixChen ,

    "Establishing a secure LDAP connection using SSL" seems what you are looking for. You can specify port number via ldap_sslinit function.

    If no port number is specified, the LDAP provider uses the default port number. The default port number is 389 if not using an SSL connection or 636 if using an SSL connection.

    For more information about specific Active Directory Domain Services technologies, see:

    • Lightweight Directory Access Protocol
    • Active Directory Service Interfaces
    • System.DirectoryServices

    Thank you!


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. FelixChen 21 Reputation points
    2020-09-25T06:31:51.577+00:00

    Hi @Rita Han - MSFT ,

    Thank you! Your information is very useful for me.

    I checked the source code of DirectoryContext, it missed the type “AuthenticationTypes.SecureSocketsLayer”:
    https://github.com/dotnet/runtime/blob/master/src/libraries/System.DirectoryServices/src/System/DirectoryServices/ActiveDirectory/Utils.cs#L49

    So cannot use DirectoryContext to connect AD with LDAPS protocol...
    Need to apply your approach.