如何在C#中通过指纹查找证书
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11115511/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find certificate by its thumbprint in C#
提问by RATHI
I am using this code to find the certificate by its thumbprint. certificate exists in certificate manager in personal certificate store but this code is not finding that certificate.
我正在使用此代码通过指纹查找证书。证书存在于个人证书存储中的证书管理器中,但此代码未找到该证书。
Please tell me where I'm doing wrong in it.
请告诉我我哪里做错了。
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string certThumbPrint = "??fe14593dd66b2406c5269d742d04b6e1ab03adb1";
X509Store certStore = new X509Store(StoreName.My, StoreLocation.CurrentUser);
// Try to open the store.
certStore.Open(OpenFlags.ReadOnly);
// Find the certificate that matches the thumbprint.
X509Certificate2Collection certCollection = certStore.Certificates.Find(
X509FindType.FindByThumbprint, certThumbPrint, false);
certStore.Close();
// Check to see if our certificate was added to the collection. If no,
// throw an error, if yes, create a certificate using it.
if (0 == certCollection.Count)
{
Console.WriteLine("Error: No certificate found containing thumbprint " );
}
Console.ReadLine();
}
采纳答案by KenD
Just stumbled over this question when Googling for the same issue, and found the answer here: if, like me, you obtained your "source" thumbprint from MMC by highlighting the thumbprint and copying it to the clipboard, you've almost certainly caught an invisible character at the start of the screen, so:
在谷歌搜索同一问题时偶然发现了这个问题,并在此处找到了答案:如果像我一样,您通过突出显示指纹并将其复制到剪贴板从 MMC 获得了“源”指纹,那么您几乎肯定会发现一个屏幕开头的不可见字符,因此:
string certThumbPrint = "??fe14593dd66b2406c5269d742d04b6e1ab03adb1";
字符串 certThumbPrint = "??fe14593dd66b2406c5269d742d04b6e1ab03adb1";
is actually
实际上是
string certThumbPrint = "??INVISIBLECHARACTERfe14593dd66b2406c5269d742d04b6e1ab03adb1";
字符串 certThumbPrint = "?? INVISIBLECHARACTERfe14593dd66b2406c5269d742d04b6e1ab03adb1";
If you delete this invisible character (you can tell it's there when you press backspace or delete beside it and nothing seems to happen), or just retype the thumbprint by hand, your code should work fine. Now if only Visual Studio had a "show invisible characters" option ...
如果您删除这个不可见的字符(当您按退格键或删除它时可以知道它在那里,但似乎什么也没有发生),或者只是手动重新键入指纹,您的代码应该可以正常工作。现在,如果只有 Visual Studio 有一个“显示不可见字符”选项......
回答by vulcanik
My two cents: I copied the value in MMC and pasted it in VS with White Spaces enabled.
我的两分钱:我复制了 MMC 中的值并将其粘贴到启用了空白的 VS 中。
There was nothing in the beginning but a space in the end: "1e 52 73 0d 00 29 e6 85 7b e6 23 e2 fa c7 a5 08 ac 36 5e 57 "
开头只有一个空格,结尾是一个空格:“1e 52 73 0d 00 29 e6 85 7b e6 23 e2 fa c7 a5 08 ac 36 5e 57”
Now, in web.config file I pasted the value maintaining all the spaces inside, removing the final space: "1e 52 73 0d 00 29 e6 85 7b e6 23 e2 fa c7 a5 08 ac 36 5e 57"
现在,在 web.config 文件中,我粘贴了保留其中所有空格的值,删除了最后一个空格:“1e 52 73 0d 00 29 e6 85 7b e6 23 e2 fa c7 a5 08 ac 36 5e 57”
This works fine.
这工作正常。
If I use "1e52730d0029e6857be623e2fac7a508ac365e57", removing the space inside as I see in other posts, doesn't work...
如果我使用“1e52730d0029e6857be623e2fac7a508ac365e57”,如我在其他帖子中看到的那样删除里面的空间,不起作用......
Hope this can help ;)
希望这可以帮助;)
回答by Edward Brey
The string literal containing your thumbprint has a left-to-right markat the beginning. When MMC lists the certificate properties, it precedes the thumbprint value with this character so that the hex bytes are listed left to right even in locales where the text is normally rendered right to left.
包含指纹的字符串文字在开头有一个从左到右的标记。当 MMC 列出证书属性时,它会在指纹值之前使用此字符,以便十六进制字节从左到右列出,即使在文本通常从右到左呈现的区域设置中也是如此。
Likely, this was a shortcut someone took because it was easier to prepend a character to one of the values in the property list than write a bit of code to dynamically update the edit control's style. Perhaps it was a quick fix to a bug report during localization testing.
很可能,这是某人采用的捷径,因为在属性列表中的一个值前面添加一个字符比编写一些代码来动态更新编辑控件的样式更容易。也许这是对本地化测试期间错误报告的快速修复。
In the MMC, the left-to-right mark has non-zero width, which you can observe by watching the cursor move when you arrow across it and my noticing that the first row of hex bytes is shifted slightly to the right compared to the second row.
在 MMC 中,从左到右的标记具有非零宽度,当您用箭头穿过它时,您可以通过观察光标移动来观察这一点,我注意到与第二行。
In other editors such as Visual Studio, the left-to-right mark has no width, but you can still observe it by noticing that the cursor does not move when you arrow across is. As KenD answered, deleting this character solves the problem.
在 Visual Studio 等其他编辑器中,从左到右的标记没有宽度,但您仍然可以通过注意到当您箭头穿过时光标没有移动来观察它。正如 KenD 所回答的那样,删除这个字符可以解决问题。
Quick way to identify the invisible character:Use the keyboard to select the invisible character; then paste it into Word between some normal characters. Select it in Word; then click Insert > Symbol > More Symbols. Look in the lower left under "Unicode name".
快速识别隐藏字符的方法:使用键盘选择隐藏字符;然后将其粘贴到 Word 中的一些普通字符之间。在 Word 中选择它;然后单击插入 > 符号 > 更多符号。在“Unicode 名称”下查看左下角。
回答by drowhunter
to ensure that those LTR "\u200e" and RTL "\u200f" chars are removed from your thumbprint string do the following
要确保从指纹字符串中删除那些 LTR "\u200e" 和 RTL "\u200f" 字符,请执行以下操作
thumbprint = thumbprint.Replace("\u200e", string.Empty).Replace("\u200f", string.Empty).Replace(" ",string.Empty);
the last string replace for the white space removal isnt completely necessary as it finds my certificate with or without them.
删除空白的最后一个字符串替换不是完全必要的,因为它可以找到我的证书,无论是否有它们。
other troublesome unicode characters can be found here
其他麻烦的 unicode 字符可以在这里找到
回答by David Valdez
I was able to resolve issue by writing a console application that retrieve all certs on certificate and output the thumbprint id. I copied the console output and inserted the thumbprint exactly. No issues. Seems like copying from the MMC console causes issues even though the data looks similar. I used this site as starting point to reading all certificates.
我能够通过编写一个控制台应用程序来解决问题,该应用程序检索证书上的所有证书并输出指纹 ID。我复制了控制台输出并准确插入了指纹。没有问题。即使数据看起来相似,从 MMC 控制台复制似乎也会导致问题。我使用这个网站作为阅读所有证书的起点。
回答by Pradeep
I run this powershell script to get all thumbprints and redirect the output to a text file and copy the thumbprint from there.
我运行这个 powershell 脚本来获取所有指纹并将输出重定向到一个文本文件并从那里复制指纹。
Get-ChildItem -path cert:\LocalMachine\My
To redirect to the output to a text file use this:
要将输出重定向到文本文件,请使用以下命令:
Get-ChildItem -path cert:\LocalMachine\My > thumbprints.txt
回答by Darren Sandford
I did the following to remove the extra character, and also to remove anything else that's not valid hexadecimal (and ToUpper it):
我做了以下操作来删除多余的字符,并删除任何其他无效的十六进制(和 ToUpper 它):
thumbprint = Regex.Replace(thumbprint.ToUpper(), @"[^0-9A-F]+", string.Empty);
This allowed me to copy the thumbprint straight from the cert manager dialog and paste it straight into my usage.
这使我可以直接从证书管理器对话框中复制指纹并将其直接粘贴到我的使用中。

