title: Try connect to AD via Ldap
body: <p>I want to implement a function which connects to AD via Ldap.</p>
  
  <p>My code:</p>
  
  <pre><code>public bool TryConnect(string server, string userName, string password)
  {
      try
      {
          var credentials = new NetworkCredential(@"test\administrator123", @"P2ssw0rd");
          var ldapidentifier = new LdapDirectoryIdentifier("Test");
  
          using (var ldapconn = new LdapConnection(ldapidentifier, credentials))
          {
              ldapconn.AuthType = AuthType.Basic;
              var request = new SearchRequest("dc=test, dc=local", "(objectClass=user)", System.DirectoryServices.Protocols.SearchScope.Subtree, null);
              var response = (SearchResponse)ldapconn.SendRequest(request);
          }
      }
      catch (Exception)
      {
          return false;
      }
  
      return true;
  }
  </code></pre>
  
  <p>This is working, but I don't like it !</p>
  
  <p>Is there some cleaner way how to test connection without try/catch and without some dummy search request?</p>