title: LDAP connection response server unavailable
body: <p>I am trying to authenticate with LDAP with various code snippet but always getting ldap server unavailable.
  While same connection when i tried to connect with LDAP browser then it worked.
  <a href="http://btechintegrator.com/index.php/2020/01/22/free-online-cloud-ldap/" rel="nofollow noreferrer">http://btechintegrator.com/index.php/2020/01/22/free-online-cloud-ldap/</a></p>
  <p><a href="https://i.stack.imgur.com/2JHRJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/2JHRJ.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/luxzh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/luxzh.png" alt="enter image description here" /></a></p>
  <p>Any idea what specific C# code snippet i should use to connect and authenticate</p>
  <p>tried following</p>
  <pre><code>try
              {
                  LdapConnection con = new LdapConnection(new LdapDirectoryIdentifier(&quot;LDAP://www.btechldap.com&quot;, 1389));
                  con.SessionOptions.SecureSocketLayer = true;
                  con.SessionOptions.VerifyServerCertificate = new VerifyServerCertificateCallback(ServerCallBack);
                  con.Credential = new NetworkCredential(&quot;cn=readonlybind,ou=admins,dc=btechsample,dc=com&quot;, &quot;btechpass&quot;);
                  con.AuthType = AuthType.Basic;
                  con.Bind();
              }
              catch (Exception ex)
              {
  
  
              }
  
  
   try
              {
                  string username = &quot;developer&quot;;
                  string password = &quot;developer_pass&quot;;
                  string filter = &quot;(&amp;(&amp;(objectclass=user)(objectcategory=person))&quot; +
                       &quot;sAMAccountName=username)&quot;;
                  NetworkCredential credentials = new NetworkCredential(username, password);
                  LdapDirectoryIdentifier directoryIdentifier =
                     new LdapDirectoryIdentifier(&quot;LDAP://shihadeh.intern/cn=developer,ou=admins,dc=shihadeh,dc=intern&quot;, 389, true, false);
                  using (LdapConnection connection =
                     new LdapConnection(directoryIdentifier, credentials, AuthType.Basic))
                  {
                      connection.Timeout = new TimeSpan(0, 0, 90);
                      connection.SessionOptions.ProtocolVersion = 3;
                      connection.Bind();
                      //SearchRequest search =
                      //    new SearchRequest(username, filter, System.DirectoryServices.Protocols.SearchScope.Base, &quot;mail&quot;);
                      //SearchResponse response = connection.SendRequest(search) as SearchResponse;
                      //foreach (SearchResultEntry entry in response.Entries)
                      //{
                      //    Console.WriteLine(entry.Attributes[&quot;mail&quot;][0]);
                      //}
                  }
              }
              catch (Exception ex)
              {
  
  
              }
  </code></pre>
  <p>I am able to connect via Java following code. needed equivalent .net one</p>
  <pre><code>public static void main(String[] args) {
          String url = &quot;ldap://localhost:389&quot;;
          Hashtable env = new Hashtable();
          env.put(Context.INITIAL_CONTEXT_FACTORY, &quot;com.sun.jndi.ldap.LdapCtxFactory&quot;);
          env.put(Context.PROVIDER_URL, url);
          env.put(Context.SECURITY_AUTHENTICATION, &quot;simple&quot;);
          env.put(Context.SECURITY_PRINCIPAL, &quot;cn=developer,dc=shihadeh,dc=intern&quot;);
          env.put(Context.SECURITY_CREDENTIALS, &quot;developer_pass&quot;);
  
          try {
              DirContext ctx = new InitialDirContext(env);
              System.out.println(&quot;connected&quot;);
              System.out.println(ctx.getEnvironment());
  
              // do something useful with the context...
  
              ctx.close();
  
          } catch (AuthenticationNotSupportedException ex) {
              System.out.println(&quot;The authentication is not supported by the server&quot;);
          } catch (AuthenticationException ex) {
              System.out.println(&quot;incorrect password or username&quot;);
          } catch (NamingException ex) {
              System.out.println(&quot;error when trying to create the context&quot;);
          }
      System.console().readLine();
      }
  </code></pre>