C# 获取证书KeyContainerName和其他属性

C# 获取证书KeyContainerName和其他属性,c#,.net,x509certificate,C#,.net,X509certificate,我正在尝试使用自定义生成的证书对XML进行签名,该证书可以通过此代码访问 private void buttonSelectCertificate_Click(object sender, EventArgs e) { CertStoreLocation = (StoreLocation)cboStoreLocation.SelectedItem; CertStoreName = (StoreName)cboStoreName.SelectedItem; X509Store s

我正在尝试使用自定义生成的证书对XML进行签名,该证书可以通过此代码访问

private void buttonSelectCertificate_Click(object sender, EventArgs e)
{
   CertStoreLocation = (StoreLocation)cboStoreLocation.SelectedItem;
   CertStoreName = (StoreName)cboStoreName.SelectedItem;
   X509Store store = new X509Store(CertStoreName, CertStoreLocation);
            store.Open(OpenFlags.ReadOnly);
   X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(store.Certificates, "Certificate Select", "Select a certificate from the following list to get information on that certificate",  System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);
}
我无法获得的内容如何正确使用
X509Certificate2Collection scollection
以填写以下属性

此外,我也没有以下想法:

如果证书存储在这里
My=5
,那么我是否使用
CSProviderFlags.UseMachineKeyStore

如何从
X509Certificate2Collection集合
获取
KeyContainerName

最后,也许我在获取用于填充
CspParameters
类所需的证书属性的方法上完全错了,有什么线索吗

谢谢你的帮助

// Get the key pair from the key store.
CspParameters parms = new CspParameters(1);         // PROV_RSA_FULL
parms.Flags = ??? CspProviderFlags.UseMachineKeyStore;  // Use Machine store
parms.KeyContainerName = ???;               // 
parms.KeyNumber = 2;                                // AT_SIGNATURE
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(parms);
这是答案

private void buttonSelectCertificate_Click(object sender, EventArgs e)
{
   CertStoreLocation = (StoreLocation)cboStoreLocation.SelectedItem;
   CertStoreName = (StoreName)cboStoreName.SelectedItem;
   X509Store store = new X509Store(CertStoreName, CertStoreLocation);
            store.Open(OpenFlags.ReadOnly);
   X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(store.Certificates, "Certificate Select", "Select a certificate from the following list to get information on that certificate",  System.Security.Cryptography.X509Certificates.X509SelectionFlag.SingleSelection);


  foreach (X509Certificate2 cert in scollection)
            {

                var rsa = cert.PrivateKey as RSACryptoServiceProvider;
                if (rsa == null) continue; // not smart card cert again

                if (!string.IsNullOrEmpty(rsa.CspKeyContainerInfo.KeyContainerName))
                {
                    // This is how we can get it! :)  
                    var keyContainerName = rsa.CspKeyContainerInfo.KeyContainerName;
                }
            }
}
此外,我们还使用了正常的
csproviderflags.UseMachineKeyStore

CspParameters parms = new CspParameters(1);         // PROV_RSA_FULL
parms.Flags = CspProviderFlags.UseMachineKeyStore;  // Use Machine store
退房-;您还可以像X509Certificate2那样执行var x=scoCollection[0]之类的操作(请原谅我的代码)