title: How to retrieve user's Private Key from Active Directory with C# (=How to get a PKCS12 (PFX) certificate from Active Directory?)
body: <p>We are implementing a digital signature ad-hoc scheme using Active Directy and C#.
  We would like to know how should we proceed to RETRIEVE the Private Key (or the user's PFX file) from the user's account entry on the AD, so that we can use that private key to sign some information.  At the same, how to properly install the PFX file for each user in AD ? (so that we can retrieve it later programatically)</p>
  
  <p>We are already accessing the AD and retrieving the Public Key information from the field "userCert", which we are now using to verify signed documents, but we need to access the private key to sign those documents first !</p>
  
  <p>We are using LDAP to access AD.  Something like this:</p>
  
  <pre><code>DirectoryEntry o2ADEntry = new DirectoryEntry("LDAP://darwin.charles/DC=domain, DC=info", "user", "password");
  DirectorySearcher o2objSearch = new System.DirectoryServices.DirectorySearcher(o2ADEntry);
  
  o2objSearch.PropertiesToLoad.Add("samaccountname");
  o2objSearch.PropertiesToLoad.Add("displayname");
  o2objSearch.PropertiesToLoad.Add("userCert");
  o2objSearch.Filter = (string.Format("(samaccountname={0})", "user")); 
  
  SearchResult res = o2objSearch.FindOne();
  if (res != null)
  {
     CreateNavigator().SelectSingleNode("/my:myFields/my:name", NamespaceManager).SetValue(res.GetDirectoryEntry().Properties["displayname"].Value.ToString());
     FormState["certificate"] = res.GetDirectoryEntry().Properties["userCert"].Value;
  }
  </code></pre>
  
  <p>We are also using standard Microsoft Crypto API (no bouncycastle, no OpenSSL.NET).</p>
  
  <p>We checked here <a href="http://technet.microsoft.com/en-us/library/aa996408(v=exchg.65).aspx" rel="nofollow">http://technet.microsoft.com/en-us/library/aa996408(v=exchg.65).aspx</a>, but nothing is said about how to access the privatekey.</p>
  
  <p>Thanks a lot, friends !</p>