title: How to get All attributes from an Active Directory user in C#
body: <p>I have been searching for quite some time for a solution using C# code that can query an Active Directory user for all the attributes it has registered to it, whether or not they have a NULL Value. These attributes are visible through the Attribute editor tab in the properties of the user in ADSI Edit on the domain server.</p>
  
  <p><a href="http://i.stack.imgur.com/5Ng9t.png" rel="nofollow">AD user attributes in ADSI edit</a></p>
  
  <p>I need to dynamically retrieve these attributes, which means I probably can't reliably get these attribute names through the ADSI documentation on MSDN and because not all of these attributes might be user object specific: 
  <a href="https://msdn.microsoft.com/en-us/library/ms675090(v=vs.85).aspx" rel="nofollow">https://msdn.microsoft.com/en-us/library/ms675090(v=vs.85).aspx</a></p>
  
  <p>Here is what I have tried so far, but only got a fraction of the attributes of the user object:</p>
  
  <ol>
  <li><p>PS command <code>Get-ADUser -Identity administrator -Properties</code>: This retrieved a good part of the attributes, but not nearly all of them and I do not know what .NET Classes and methods are invoked during this command, since <code>TypeName = Microsoft.ActiveDirectory.Management.ADUser</code>, which does not exist in the .NET framework. How can I see the specific methods that are using from .NET in PS?</p></li>
  <li><p>C# calling this method:</p>
  
  <pre><code>public bool GetUserAttributes(out List&lt;string&gt; userAttributes, string userName)
  {
      userAttributes = new List&lt;string&gt;();
      var valueReturn = false;
  
      try
      {
          const string pathNameDomain = "LDAP://test.local";
  
          var directoryEntry = new DirectoryEntry(pathNameDomain);
  
          var directorySearcher = new DirectorySearcher(directoryEntry)
          {
              Filter = "(&amp;(objectClass=user)(sAMAccountName=" + userName + "))"
          };
  
          var searchResults = directorySearcher.FindAll();
  
          valueReturn = searchResults.Count &gt; 0;
  
          StreamWriter writer = new StreamWriter("C:\\LDAPGETUSERADEXAMPLE.txt");
  
          foreach (SearchResult searchResult in searchResults)
          {
              foreach (var valueCollection in searchResult.Properties.PropertyNames)
              {
                  userAttributes.Add(valueCollection.ToString() + " = " + searchResult.Properties[valueCollection.ToString()][0].ToString());
  
                  try
                  {
                      writer.WriteLine("Bruger attribut:" + valueCollection);
                  }
                  catch (Exception)
                  {
                      throw;
                  }
              }
          }
  </code></pre></li>
  <li><p>C# calling this method:</p>
  
  <pre><code>public List&lt;string&gt; GetADUserAttributes()
  {
      string objectDn = "CN=testuser,OU=TEST,DC=test,DC=local";
  
      DirectoryEntry objRootDSE = new DirectoryEntry("LDAP://" + objectDn);
      List&lt;string&gt; attributes = new List&lt;string&gt;();
  
      foreach (string attribute in objRootDSE.Properties.PropertyNames)
      {
          attributes.Add(attribute);
      }
  
      return attributes;
  }
  </code></pre></li>
  </ol>
  
  <p>What should I do to not filter out any attributes of the user object I am trying to retrieve from?</p>
  
  <p>I am aware that Active Directory by default will only shows attributes that are default or have a value in them, I am trying to overcome this limitation.</p>
  
  <p><strong>EDIT 1:</strong></p>
  
  <p>I have temporarily postponed the specific question.
  I have been trying to benchmark which of these methods are the fastest at retrieving (READ Operation) the SAM account name of 10.000 individual AD users called for example "testuser", the methods I benchmark are the following:</p>
  
  <ol>
  <li>Time to complete: about 500 msec : ADSI - system.directoryservices</li>
  <li>Time to complete: about 2700 msec: Principal - searcher system.directoryservices.accountmanagement</li>
  <li>Time to complete: about NOT WORKING :LDAP - System.DirectoryServices.Protocols</li>
  <li>Time to complete: about 60 msec : SQL - System.Data.SqlClient</li>
  </ol>
  
  <p>I am querying for the user information from a workstation - Windows 10 machine in the domain I am querying. the workstation (4 vcpu), DC (2vpu) and DB (2vcpu) server is run as Hyper V vm's. </p>