如何通过其在C#指纹找到证书 [英] How to find certificate by its thumbprint in C#

查看:735
本文介绍了如何通过其在C#指纹找到证书的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code发现其指纹的证书。证书在个人证书存储区证书管理器存在,但这个code未发现该证书。

请告诉我,我做错了它。

 命名空间的ConsoleApplication1
{
    类节目
    {
        静态无效的主要(字串[] args)
        {
    字符串certThumbPrint =fe14593dd66b2406c5269d742d04b6e1ab03adb1;
            的X509Store的CertStore =新的X509Store(StoreName.My,StoreLocation.CurrentUser);
            //尝试开实体店。            certStore.Open(OpenFlags.ReadOnly);
            //查找指纹匹配的证书。
            X509Certificate2Collection certCollection = certStore.Certificates.Find(X509FindType.FindByThumbprint,certThumbPrint,FALSE);
            certStore.Close();            //检查是否我们的证书添加到集合。如果没有,抛出一个错误,如果是,创建一个使用它的证书。
            如果(0 == certCollection.Count)
            {
                Console.WriteLine(错误:未找到包含指纹证书);
            }
            到Console.ReadLine();
}


解决方案

谷歌搜索同样的问题时,只是绊了这个问题,并找到了答案<一个href=\"http://stackoverflow.com/questions/8448147/problems-with-x509store-certificates-find-findbythumbprint\">here:如果像我一样,你通过突出显示指纹并将其复制到剪贴板获得从MMC你的源指纹,你几乎肯定会陷入一个无形的字符在屏幕的开始,所以:


  

字符串certThumbPrint =fe14593dd66b2406c5269d742d04b6e1ab03adb1;


其实就是


  

字符串certThumbPrint =*的 INVISIBLECHARACTER 的* fe14593dd66b2406c5269d742d04b6e1ab03adb1;


如果您删除此不可见字符(你可以告诉它的存在,当你preSS退格或删除其旁边并没有什么似乎发生),或者只是重新键入手指纹,你的code应该可以正常工作。现在,如果只有Visual Studio中有一个显示不可见字符选项...

i am using this code to find the certificate by its thumbprint. certificate exist 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();
}

解决方案

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:

string certThumbPrint = "‎‎fe14593dd66b2406c5269d742d04b6e1ab03adb1";

is actually

string certThumbPrint = "‎‎*INVISIBLECHARACTER*fe14593dd66b2406c5269d742d04b6e1ab03adb1";

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 ...

这篇关于如何通过其在C#指纹找到证书的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆