0

I have one issue when trying to connect to the LDAP server through code. It works fine when I use admin tool to connect to it.

admin tool

it works fine when using this admin tool to connect to it. it doesn't work when I use this code to connect to it, it says The server could not be contacted. ---> System.DirectoryServices.Protocols.LdapException: The LDAP server is unavailable. My code:

Using context As DirectoryServices.AccountManagement.PrincipalContext = New DirectoryServices.AccountManagement.PrincipalContext(DirectoryServices.AccountManagement.ContextType.Domain, SingleSignOn.ADDomain, SingleSignOn.ADSecurityGroup, DirectoryServices.AccountManagement.ContextOptions.SecureSocketLayer Or DirectoryServices.AccountManagement.ContextOptions.Negotiate, UserName, Password)

                    Using foundUser = DirectoryServices.AccountManagement.UserPrincipal.FindByIdentity(context, UserName)
                        Return foundUser IsNot Nothing
                    End Using

                End Using

My question is: how to set up the code to use version 3? Thank you in advance for your help/ideas.

LoverBugs
  • 127
  • 1
  • 2
  • 14
  • It's possible that the certificate isn't trusted by Windows (and LDAP Admin doesn't care). Check [this answer](https://stackoverflow.com/a/62895231/1202807) for instructions on how to check the certificate. – Gabriel Luci Dec 09 '21 at 16:26
  • when I run that bat script, it says '$webRequest' is not recognized as an internal or external command, operable program or batch file. – LoverBugs Dec 09 '21 at 16:51
  • @GabrielLuci when I use this tool, first it shows this message https://prnt.sc/22hg100; saying the certificate is self-signed.... after hitting the Yes button it connects ... – LoverBugs Dec 09 '21 at 16:58
  • It's not a batch file, it's a PowerShell script. You need to run it in a PowerShell window. The certificate is definitely your problem then. – Gabriel Luci Dec 09 '21 at 17:16

1 Answers1

0

Windows needs to trust the SSL certificate, otherwise the connection will fail. Unfortunately the error message doesn't tell you that.

You have a couple options:

  1. Change the certificate being used on the server to a certificate from a trusted root authority. This is the best way to do it, especially if this is a production server.

  2. Tell Windows to trust the self-signed cert. This would have to be done on every computer that will connect. To do this, use the PowerShell script in this answer to download the certificate (change the URL to match your server). This will give you a .cer file. Then follow the instructions here to import it on the computer that you are running this code on. In that article, start at the heading "To start the certificate import process through Microsoft Management Console (MMC)". In step 4, you have the option to import it for the current user only, or for the whole computer (which requires local admin rights).

Gabriel Luci
  • 38,328
  • 4
  • 55
  • 84