public static Find ( |
||
collection | The collection. | |
thumbprint | string | The thumbprint of the certificate. |
subjectName | string | Subject name of the certificate. |
needPrivateKey | bool | if set to |
return |
public static X509Certificate2 Find(X509Certificate2Collection collection, string thumbprint, string subjectName, bool needPrivateKey)
{
// find by thumbprint.
if (!String.IsNullOrEmpty(thumbprint))
{
collection = collection.Find(X509FindType.FindByThumbprint, thumbprint, false);
foreach (X509Certificate2 certificate in collection)
{
if (!needPrivateKey || certificate.HasPrivateKey)
{
if (String.IsNullOrEmpty(subjectName))
{
return certificate;
}
List<string> subjectName2 = Utils.ParseDistinguishedName(subjectName);
if (Utils.CompareDistinguishedName(certificate, subjectName2))
{
return certificate;
}
}
}
return null;
}
// find by subject name.
if (!String.IsNullOrEmpty(subjectName))
{
List<string> subjectName2 = Utils.ParseDistinguishedName(subjectName);
foreach (X509Certificate2 certificate in collection)
{
if (Utils.CompareDistinguishedName(certificate, subjectName2))
{
if (!needPrivateKey || certificate.HasPrivateKey)
{
return certificate;
}
}
}
collection = collection.Find(X509FindType.FindBySubjectName, subjectName, false);
foreach (X509Certificate2 certificate in collection)
{
if (!needPrivateKey || certificate.HasPrivateKey)
{
return certificate;
}
}
}
// certificate not found.
return null;
}
/// <summary> /// Returns the issuers for the certificate. /// </summary> /// <param name="certificate">The certificate.</param> /// <param name="issuers">The issuers.</param> /// <returns></returns> public async Task <bool> GetIssuers(X509Certificate2 certificate, List <CertificateIdentifier> issuers) { bool isTrusted = false; CertificateIdentifier issuer = null; do { issuer = await GetIssuer(certificate, m_trustedCertificateList, m_trustedCertificateStore, true); if (issuer == null) { issuer = await GetIssuer(certificate, m_issuerCertificateList, m_issuerCertificateStore, true); } else { isTrusted = true; } if (issuer != null) { issuers.Add(issuer); certificate = await issuer.Find(false); // check for root. if (Utils.CompareDistinguishedName(certificate.Subject, certificate.Issuer)) { break; } } }while (issuer != null); return(isTrusted); }