1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public byte[] GetRecipientCertificateFromLDAPStore()
{
SearchResultCollection col;
DirectorySearcher searcher = new DirectorySearcher();
string[] resultsFields = new string[] { "cn", "mail", "usercertificate;binary" };
//Pass the IPAddress and the Port of the LDAP Server.
string[] textArray1 = new string[] { "LDAP://", "ldapclient.com", ":", "636", "" };
searcher.SearchRoot = new DirectoryEntry(string.Concat(textArray1), null, null, AuthenticationTypes.SecureSocketsLayer);
searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
searcher.PropertiesToLoad.AddRange(resultsFields);
searcher.Filter = string.Format("(&(cn={0})(mail={1}))", "* *", "* *");
col = searcher.FindAll();
X509Certificate2 certificate1 = new X509Certificate2();
foreach (SearchResult result1 in col)
{
IEnumerator enumerator2;
try
{
enumerator2 = result1.GetDirectoryEntry().Properties["usercertificate;binary"].GetEnumerator();
while (enumerator2.MoveNext())
{
object obj1 = RuntimeHelpers.GetObjectValue(enumerator2.Current);
certificate1.Import((byte[])obj1);
//Can access different Properties for example:
//certificate1.Subject;
//certificate1.SerialNumber;
//certificate1.Version;
//certificate1.NotBefore;
//certificate1.NotAfter;
//certificate1.Issuer;
return certificate1.Export(X509ContentType.Cert);
}
}
catch { }
}
return null;
} |
Partager