title: How to get all the user's details from Active Directory using LDAP
body: <p>I need to get all the user's details from Active directory using LDAP. The following code does gives <code>Samaccountname</code> as 'Administrator' but not each user's details and no mail ID is found in the list. Kindly Help.</p>
  
  <pre><code>string dominName = ConfigurationManager.AppSettings["DominName"].ToString();
  string ldapPath = ConfigurationManager.AppSettings["ldapPath"].ToString();
  if (!String.IsNullOrEmpty(dominName) &amp;&amp; !String.IsNullOrEmpty(ldapPath))
  {
      DirectoryEntry entry = new DirectoryEntry(ldapPath, txtUsername.Text.ToString().Trim(), txtPassword.Text.ToString().Trim());
      try
      {
          Object obj = entry.NativeObject;
          DirectorySearcher search = new DirectorySearcher(entry);
          search.Filter = "(&amp;(objectClass=user)(objectCategory=person))";
          search.PropertiesToLoad.Add("samaccountname");
          search.PropertiesToLoad.Add("mail");
          search.PropertiesToLoad.Add("usergroup");
          search.PropertiesToLoad.Add("displayname");//first name
  
          foreach (System.DirectoryServices.SearchResult resEnt in search.FindAll())
          {    
              System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry();
              if (de.Properties["sAMAccountName"].Value != null &amp;&amp; de.Properties["userAccountControl"].Value!=null)
              {
                  StringBuilder sb = new StringBuilder();
                  sb.AppendLine("Name = " + de.Properties["sAMAccountName"].Value.ToString());
                  sb.AppendLine("Email = " + de.Properties["Mail"].Value.ToString());
              }
          }
  </code></pre>
  
  <p>Found Solution:</p>
  
  <p>Here is my code:</p>
  
  <pre><code>var userAccountControlValue = 0;
  int.TryParse(de.Properties["UserAccountControl"].Value.ToString(), out userAccountControlValue);
  var isAccountDisabled = Convert.ToBoolean(userAccountControlValue &amp; 0x0002);
  var isNormalAccount = Convert.ToBoolean(userAccountControlValue &amp; 0x0200);
  if (de.Properties["sAMAccountName"].Value != null &amp;&amp; de.Properties["userAccountControl"].Value != null &amp;&amp; de.Properties["userPrincipalName"].Value != null &amp;&amp; !isAccountDisabled &amp;&amp; isNormalAccount)
  {
      //Add Employee details from AD
      PaySlipPortal.Objects.Employee employee = new Employee();
      employee.FirstName = de.Properties["givenName"].Value!=null?(string)de.Properties["givenName"].Value:"";
      employee.Email = de.Properties["userPrincipalName"].Value != null ? (string)de.Properties["userPrincipalName"].Value : "";
      employee.LastName = de.Properties["sn"].Value != null ? 
  
      (string)de.Properties["sn"].Value : "";
      int deleteID=  empBL.DeleteEmployee(employee.Email.Trim());
      int empID = empBL.AddEmployee(employee);  
  
  }
  </code></pre>