private static X509Certificate2 FindCertificate(string certificateSubject)
{
const StoreName StoreName = StoreName.My;
const StoreLocation StoreLocation = StoreLocation.LocalMachine;
var store = new X509Store(StoreName, StoreLocation);
try
{
store.Open(OpenFlags.ReadOnly);
// Find with the FindBySubjectName does fetch all the certs partially matching the subject name.
// Hence, further filter for the certs that match the exact subject name.
List<X509Certificate2> clientCertificates =
store.Certificates.Find(X509FindType.FindBySubjectName, certificateSubject, validOnly: true)
.Cast<X509Certificate2>()
.Where(c => string.Equals(
c.Subject.Split(',').First().Trim(),
string.Concat("CN=", certificateSubject).Trim(),
StringComparison.OrdinalIgnoreCase)).ToList();
if (!clientCertificates.Any())
{
throw new InvalidDataException(
string.Format(CultureInfo.InvariantCulture, "Certificate {0} not found in the store {1}.", certificateSubject, StoreLocation.LocalMachine));
}
X509Certificate2 result = null;
foreach (X509Certificate2 cert in clientCertificates)
{
DateTime now = DateTime.Now;
DateTime effectiveDate = DateTime.Parse(cert.GetEffectiveDateString(), CultureInfo.CurrentCulture);
DateTime expirationDate = DateTime.Parse(cert.GetExpirationDateString(), CultureInfo.CurrentCulture);
if (effectiveDate <= now && expirationDate.Subtract(now) >= TimeSpan.FromDays(1))
{
result = cert;
break;
}
}
return result;
}
finally
{
store.Close();
}
}我的库中有这段代码,每次创建新请求时,它都会调用此方法。所以,基本上,每秒的请求是1000,那么它将被调用1000次。当我使用PerfView工具时,我注意到35%的CPU是通过这种方法使用的。最大的罪魁祸首是store.Open和store.Certificates.Find方法。
其他人在代码中发现了类似的问题。此外,如果您可以共享您做了什么来解决由于此而造成的性能影响。
发布于 2015-11-05 21:42:28
只要目标系统没有安装大量证书,就可以跳过对X509Store的.Find()方法的调用。根据我的经验,它的性能不太好,之后您已经对目标subjectName进行了必要的筛选。
另外,不要在X509Certificate2集合中循环两次!如果您只需要满足所有条件的第一个匹配证书,可以将事情简化为一个LINQ语句,如下所示:
X509Certificate2 cert =
store.Certificates.Cast<X509Certificate2>()
.FirstOrDefault(xc =>
xc.Subject.Equals("CN=" + certificateSubject, StringComparison.OrdinalIgnoreCase)
&& xc.NotAfter >= DateTime.Now.AddDays(-1)
&& xc.NotBefore <= DateTime.Now);(请注意,根据您的用法和证书,您可能需要或不需要修改上面的内容,以便像原来的代码那样以逗号分隔主题)。
最后,如前所述,如果您的目标计算机没有安装大量证书,则可以通过调用store.Certificates.Cast<X509Certificate2>().ToList()缓存整个证书列表,或者如果您搜索的subjectNames数量有限,则使用从subjectName派生的密钥缓存此方法的结果并基于NotAfter属性进行过期可能更有效。
https://stackoverflow.com/questions/33554122
复制相似问题