C# 如何访问c中具有相同公用名称的两个证书中的最新证书#

C# 如何访问c中具有相同公用名称的两个证书中的最新证书#,c#,x509certificate,x509certficiate2,C#,X509certificate,X509certficiate2,如何从c#中的windows证书存储中访问具有相同公用名的两个证书中最近的证书或稍后将过期的证书。我正在使用X509Store.Certificates.Find获取证书,但它会返回证书列表,并且它们具有相同的CN名称,但我需要最新的证书 PS:我不想通过指纹访问它,因为每次证书过期时我都必须更改它以下功能将接受证书数组并返回最后过期的证书。它通过根据到期日期按降序创建证书的有序列表来实现 该函数不假设每个证书的有效状态 public Certificate getLongestLastingC

如何从c#中的windows证书存储中访问具有相同公用名的两个证书中最近的证书或稍后将过期的证书。我正在使用X509Store.Certificates.Find获取证书,但它会返回证书列表,并且它们具有相同的CN名称,但我需要最新的证书


PS:我不想通过指纹访问它,因为每次证书过期时我都必须更改它

以下功能将接受证书数组并返回最后过期的证书。它通过根据到期日期按降序创建证书的有序列表来实现

该函数不假设每个证书的有效状态

public Certificate getLongestLastingCertificate(Certificate[] certs)
{
    Certificate longetsLastingCert = null;
    if (certs != null)
    {
        if (certs.Count > 0)
        {
            longetsLastingCert  = certs.OrderByDescending(o => o.GetExpirationDate()).First();
        }
    }
    return longetsLastingCert;
}

下面的函数将接受证书数组并返回最后一个过期的证书。它通过根据到期日期按降序创建证书的有序列表来实现

该函数不假设每个证书的有效状态

public Certificate getLongestLastingCertificate(Certificate[] certs)
{
    Certificate longetsLastingCert = null;
    if (certs != null)
    {
        if (certs.Count > 0)
        {
            longetsLastingCert  = certs.OrderByDescending(o => o.GetExpirationDate()).First();
        }
    }
    return longetsLastingCert;
}

NotAfter
property降序排列。按
NotAfter
property降序排列。@bartonjs同意。我已经根据你的评论修改了代码。@bartonjs同意。我已经根据你的评论修改了代码。