title: LDAP server could not be contacted when connecting using C#
body: <p>I have written the below code to connect to LDAP server and validate the user credentials. </p>
  
  <pre><code>    public static string AuthFunction_One(string identity, string password, string containerString, string adServerName, bool useLDAPS, IdentityType identityType)
      {
  
          string failedString = "FAILED";
          string successString = "SUCCESS";
          string returnValue = failedString;
          try
          {
              PrincipalContext ctx = null;
  
              ctx = new PrincipalContext(ContextType.Domain, "ldap://localhost:10389/dc=example,dc=com", "uid=rish,dc=example,dc=com");
  
              UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(ctx, identityType, identity);
              PrincipalSearchResult&lt;Principal&gt; oPrincipalSearchResult = oUserPrincipal.GetGroups();
              if (ctx.ValidateCredentials(identity, password))
              {
                  return successString;
              }
              else
              {
                  return failedString;
              }
          }
          catch (Exception ex)
          { 
              NLogHelper.GetInstance().Log("ADUtilityClass", "AuthFunction_One", NLog.LogLevel.Debug, "Error in function. Ex: " + ex.ToString());
              return failedString;
          }
      }
  </code></pre>
  
  <p>This is throwing the below exception.</p>
  
  <pre><code>Exception: Exception thrown: 
  'System.DirectoryServices.AccountManagement.PrincipalServerDownException' in System.DirectoryServices.AccountManagement.dll ("The server could not be contacted.")
  </code></pre>
  
  <p>Below is the LDAP server I am running. 
  <a href="https://i.stack.imgur.com/j5MmI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/j5MmI.png" alt="enter image description here"></a></p>
  
  <p>When I try to connect through LDAP explorer, it does connect.Below are the configurations I used there.</p>
  
  <pre><code>userdn -&gt; uid=rish,dc=example,dc=com
  basedn -&gt; dc=example,dc=com
  password -&gt; secret
  servername -&gt; localhost
  port -&gt; 10389
  version -&gt; 3
  </code></pre>
  
  <p>What am I doing wrong in my C# code? any help would be much appreciated.</p>