如何通过IIS从eToken获取“私钥”

时间:2019-09-15 13:03:23

标签: .net certificate token x509certificate2

通过USB令牌对Signature App进行编码时,我必须面对一些问题。我的证书无法导出 Current_User证书中的私钥和位置。我使用X509Certificate2从usb令牌获取证书,它可以在IIS Express上运行,但不能在IIS上运行。

我知道我必须将我的帐户角色分配给AppicationPool。我为文件夹分配了角色: C:\ ProgramData \ Microsoft \ Crypto \ RSA \ MachineKeys 。我已经用IIS运行了Visual Studio。

我刚在IIS上收到错误。仍然可以与控制台应用程序和IIS Express一起很好地工作。因此我推测IIS无法访问USB令牌,但是我不知道如何?A

这是我的代码:

var store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);

            X509Certificate2Collection certCollection = store.Certificates;
            var certificates = certCollection.Find(X509FindType.FindBySerialNumber, SerialNumber, false);
            var cert = certificates[0];

            store.Close();
            var pass = new SecureString();
            char[] array = Password.ToCharArray();
            foreach (char ch in array)
            {
                pass.AppendChar(ch);
            }
            // This line error
            var privateKey = cert.PrivateKey as RSACryptoServiceProvider;
.....
**This is result:**
<Error>
<Message>An error has occurred.</Message>
<Ex`enter code here`ceptionMessage>Keyset does not exist </ExceptionMessage>
<ExceptionType>
System.Security.Cryptography.CryptographicException
</ExceptionType>

更新: 我通过PKCS#11(使用PKCS11Interop并将应用程序池设置为localSerives)解决了此问题。 C:\ Windows \ System32 \ name_token.dll中的库路径。

0 个答案:

没有答案