title: LDAP path problems
body: <p>I am working with LDAP and I am new to this.</p>
  
  <p>Is there a way to get the domain when you only know the username, password, servername</p>
  
  <p>I am trying to do this:</p>
  
  <pre><code>string ldapPath = "LDAP://serverName";
  string uid = username;
  string password = pwd;
  string qry = String.Format("(uid={0})", uid);
  string adsPath = String.Empty;
  
  try
  {
      DirectoryEntry nRoot = new DirectoryEntry(ldapPath, null, null, AuthenticationTypes.Anonymous);
  
      DirectorySearcher ds = new DirectorySearcher(nRoot, qry);
      SearchResult sr = ds.FindOne();
  
      if (sr != null)
      {
         // we want to retrieve the DN like this: "uid=myuser,ou=People,dc=findlay,dc=edu
         ldapPath = sr.Path; //update where we will bind next
      }
  </code></pre>
  
  <p>This does not work unless I change </p>
  
  <pre><code> string ldapPath = "LDAP://serverName";
  </code></pre>
  
  <p>to</p>
  
  <pre><code> string ldapPath = "LDAP://serverName/DC=mydomain,DC=com";
  </code></pre>
  
  <p>Any help..??</p>
  
  <p>Thanks</p>
  
  <p>Edit rootDSE</p>
  
  <pre><code>string defaultNamingContext;
  
  using (DirectoryEntry rootDSE = new DirectoryEntry("LDAP://serverName/rootDSE", null, null, AuthenticationTypes.Anonymous))
  {
      defaultNamingContext = rootDSE.Properties["rootDomainNamingContext"].Value.ToString();
  }
  </code></pre>
  
  <p>I too feel this is the solution but it is currently not working for me.. please help!</p>