X509Store.Certificates.Find( FindBySerialNumber ) 不匹配
X509Store.Certificates.Find( FindBySerialNumber ) not matching
在开始之前,这不是此 QA 的副本 (How to find certificate by its thumbprint in C#) - 我使用的指纹字符串长度为 40 个字符,不包含从 MMC 复制的任何隐藏字符。
这是我使用的代码:
String thumbprint = "c112345678904655585e8c8244af5d3f2630498b".ToUpperInvariant();
assert( thumbprint.Length == 40 );
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection matches = store.Certificates.Find(X509.FindType.FindBySerialNumber, thumbprint, validOnly: false );
assert( matches.Count == 1 ); // this fails, the count is == 0.
但是当我手动查询证书时,没问题:
assert( store.Certificates.Count == 1 ); // there is only 1 cert in the store
X509Certificate2 cert = store.Certificates[0];
assert( cert.Thumbprint == thumbprint ); // this passes
这可能是什么原因造成的?为什么在完全匹配时 Find
不是 return 证书?
此 MSDN 博客 post 描述相同,但未提供解释:https://blogs.msdn.microsoft.com/avkashchauhan/2011/11/19/what-to-do-when-your-code-could-not-find-the-certificate-in-azure-vm/
我在没有意识到的情况下使用了 X509.FindType.FindBySerialNumber
而不是 X509.FindType.FindByThumbprint
。现在可以使用了。
在开始之前,这不是此 QA 的副本 (How to find certificate by its thumbprint in C#) - 我使用的指纹字符串长度为 40 个字符,不包含从 MMC 复制的任何隐藏字符。
这是我使用的代码:
String thumbprint = "c112345678904655585e8c8244af5d3f2630498b".ToUpperInvariant();
assert( thumbprint.Length == 40 );
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
X509Certificate2Collection matches = store.Certificates.Find(X509.FindType.FindBySerialNumber, thumbprint, validOnly: false );
assert( matches.Count == 1 ); // this fails, the count is == 0.
但是当我手动查询证书时,没问题:
assert( store.Certificates.Count == 1 ); // there is only 1 cert in the store
X509Certificate2 cert = store.Certificates[0];
assert( cert.Thumbprint == thumbprint ); // this passes
这可能是什么原因造成的?为什么在完全匹配时 Find
不是 return 证书?
此 MSDN 博客 post 描述相同,但未提供解释:https://blogs.msdn.microsoft.com/avkashchauhan/2011/11/19/what-to-do-when-your-code-could-not-find-the-certificate-in-azure-vm/
我在没有意识到的情况下使用了 X509.FindType.FindBySerialNumber
而不是 X509.FindType.FindByThumbprint
。现在可以使用了。