سؤال

System can't find certificate by string thumbprint

       var thumbprint = "‎2E7F6E8A0124E6745C3999EE15770C0A4931F342";
        X509Certificate2 certificate = new X509Certificate2();
        X509Store store = new X509Store(StoreName.TrustedPeople, StoreLocation.CurrentUser);
        store.Open(OpenFlags.OpenExistingOnly);
        var c = store.Certificates.Find(X509FindType.FindByThumbprint, thumbprint, false).OfType<X509Certificate>().FirstOrDefault();

this core returns null. But I try this too

foreach (X509Certificate2 mCert in store.Certificates)
        {

           var c= store.Certificates.Find(X509FindType.FindByThumbprint, mCert.Thumbprint, false).Count;
        }

c is always 1 , so some problem is in characters. I copied this thumbprint value.

هل كانت مفيدة؟

المحلول

The following works for me:

    public async Task<X509Certificate2> GetCertificate(string certificateThumbprint)
    {
        var store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly);
        var cert = store.Certificates.OfType<X509Certificate2>()
            .FirstOrDefault(x => x.Thumbprint == certificateThumbprint);
        store.Close();
        return cert;
    }
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top