尝试从Windows certstore读取PrivateKey时发生异常
||
我使用OpenSSL创建了私钥和公钥对,然后生成了.p12文件,将其导入到Windows certstore中。密钥对和.p12文件是在Windows XP中创建的,我正在尝试在Windows 7中使用它。
我正在尝试从IIS中的Web服务(.svc)内部访问密钥。
如果尝试从独立应用程序读取私钥,则可以毫无问题地进行操作,但是当我尝试从Web应用程序读取私钥时,总是会遇到以下异常:
\'cert.PrivateKey\' threw an exception of type \'System.Security.Cryptography.CryptographicException\'
这是整个堆栈跟踪:
en System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer)
en System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle)
en System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair()
en System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize)
en System.Security.Cryptography.X509Certificates.X509Certificate2.get_PrivateKey()
en ValidKeyDll.ValidKey.getLlaveDeAlmacen(String almacen, Boolean esLlavePrivada) en C:\\Users\\desarrollo\\Documents\\ValidKeyDll\\ValidKeyDll\\ValidKey.cs:línea 58
en ValidKeyDll.ValidKey.firmaCadena(String almacen, String cadenaFirmar) en C:\\Users\\desarrollo\\Documents\\ValidKeyDll\\ValidKeyDll\\ValidKey.cs:línea 117
这是我读取密钥的代码的一部分:
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
RSACryptoServiceProvider csp = null;
foreach (X509Certificate2 cert in store.Certificates)
{
if (cert.Subject.Contains(almacen))
{
if (cert.NotAfter.CompareTo(System.DateTime.Now) <= 0)
throw new CertificadoVencidoException();
if (isPrivateKey)
csp = (RSACryptoServiceProvider)cert.PrivateKey;
else
csp = (RSACryptoServiceProvider)cert.PublicKey.Key;
break;
}
}
我想它与某种许可问题有关,但我无法查明它是什么...如果有人有任何建议,将不胜感激。
注意事项:
私钥是可导出的。
用户IIS_IUSRS对证书具有权限。
没有找到相关结果
已邀请:
2 个回复
功飘
黎喊病