我有一个使用 RSACryptoServiceProvider 使用已知私钥(存储在变量中)解密一些数据的应用程序。
当 IIS 应用程序池配置为使用网络服务时,一切运行正常。
但是,当我们配置 IIS 应用程序池以在不同的身份下运行代码时,我们会得到以下信息:
System.Security.Cryptography.CryptographicException: The system cannot find the file specified. at System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) at System.Security.Cryptography.RSACryptoServiceProvider.ImportParameters(RSAParameters parameters) at System.Security.Cryptography.RSA.FromXmlString(String xmlString)
The code is something like this:
byte[] input;
byte[] output;
string private_key_xml;
var provider = new System.Cryptography.RSACryptoServiceProvider(this.m_key.Key_Size);
provider.FromXmlString(private_key_xml); // Fails Here when Application Pool Identity != Network Service
ouput = provider.Decrypt(input, false); // False = Use PKCS#1 v1.5 Padding
有一些资源试图通过说明您应该授予用户对机器 key 存储的读取权限来回答它 - 但是没有明确的答案来解决这个问题。
环境:IIS 6.0、Windows Server 2003 R2、.NET 3.5 SP1
最佳答案
我通过在应用程序池的高级设置/进程模型部分将“加载用户配置文件”设置为 True(为 False)来解决此问题。
该应用程序在 Server 2003 R2/IIS 6 上运行良好,当我在新的 2008 R2 服务器上配置它时出现了问题。
有了尝试的想法:
http://social.msdn.microsoft.com/forums/en-US/clr/thread/7ea48fd0-8d6b-43ed-b272-1a0249ae490f/
YMMV
关于.net - RSACryptoServiceProvider CryptographicException 系统找不到 ASP.NET 下指定的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1102884/