title: Can not retrieve Active Directory user in C#
body: <p>I built a test Active Directory server in Window 2008 and I also run the DNS server on it.  On my client machine which runs the C# application, I can authenticate the user against the Active directory server using the function below:</p>
  
  <pre><code>public static UserPrincipal GetUserPrincipal(string usrName,string pswd,string domainName)
  {
     UserPrincipal usr;
     PrincipalContext ad;
  
     // Enter Active Directory settings
     ad = new PrincipalContext(ContextType.Domain, domainName,usrName,pswd);
  
     //search user
     usr = new UserPrincipal(ad);
     usr.SamAccountName = usrName;
  
     PrincipalSearcher search = new PrincipalSearcher(usr);
     usr = (UserPrincipal)search.FindOne();
     search.Dispose();
     return usr;
  }
  </code></pre>
  
  <p>In a separate logic I tried to retrieve a user back from the server using a user name.  I used the functions below:</p>
  
  <pre><code>public static DirectoryEntry CreateDirectoryEntry()
  {
     // create AD connection
     DirectoryEntry de = new DirectoryEntry("LDAP://CN=Users,DC=rootforest,DC=com","LDAP","password");
     de.AuthenticationType = AuthenticationTypes.Secure;
     return de;
  }
  
  public static ResultPropertyCollection GetUserProperty(string domainName, string usrName)
  {
      DirectoryEntry de = CreateDirectoryEntry();
      DirectorySearcher deSearch = new DirectorySearcher();
      deSearch.SearchRoot = de;
      deSearch.Filter = "(SamAccountName=" + usrName + ")";
      SearchResult results = deSearch.FindOne();
  
      return null;
  }
  </code></pre>
  
  <p>However, I got no response back from the LDAP server at all, not even an exception.  Am I missing certain settings on LDAP server, any of you able to see a flaw in my code (pls don't mind the hard code values, I was testing with this code).</p>
  
  <p>As part of my troubleshooting, I confirmed that I can ping to the rootforest.com from the client machine.  I confirmed the user with property samaccountname "LDAP" exists.  My path seems to be right because when I go onto the LDAP server and type :</p>
  
  <pre><code>dsquery user -name LDAP*      
  </code></pre>
  
  <p>I got the following:</p>
  
  <pre><code>CN=LDAP L. LDAP,CN=Users,DC=rootforest,DC=com
  </code></pre>
  
  <p>Any help would be greatly appreciated, I've spent most of my day troubleshooting and researching this little bugger and I think it could be something small which I overlooked.</p>