'Cannot find the requested object' exception while creating X509Certificate2 from string

前端 未结 2 1053
时光取名叫无心
时光取名叫无心 2021-01-11 12:36

I am trying to create X509Certificate2 from string. Let me show an example:

string keyBase64String = Convert.ToBase64String(file.PKCS7);
var cer         


        
2条回答
  •  再見小時候
    2021-01-11 13:09

    The constructor of of X509Certificate2 expects to get a the certificate file name, but you are giving it a key (X509Certificate2 Constructor (String))

    I assume that keyBase64String is the certificate key, and that the certificate is installed on the machine that executes the code. Try this:

    var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
    store.Open(OpenFlags.ReadOnly);
    var certCollection = store.Certificates.Find(X509FindType.FindByThumbprint, keyBase64String , false);
    //var certCollection = store.Certificates.Find(X509FindType.FindByKeyUsage, keyBase64String , false);
    //var certCollection = store.Certificates.Find(X509FindType.FindBySubjectKeyIdentifier, keyBase64String , false);
    var cert = certCollection[0];
    

    You can also try FindByKeyUsage, FindBySubjectKeyIdentifier, or other types of X509FindType Enumeration

提交回复
热议问题