我有一个应用程序,正在使用RSACryptoServiceProvider来解密一些数据,使用已知的私钥(存储在变量中)。
当IIS应用程序池配置为使用Network Service时,一切都运行良好。
然而,当我们将IIS应用程序池配置为在不同的身份下运行代码时,我们会遇到以下问题:
System.Security.Cryptography.CryptographicException:系统找不到指定的文件。
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)
代码类似于这样:
当IIS应用程序池配置为使用Network Service时,一切都运行良好。
然而,当我们将IIS应用程序池配置为在不同的身份下运行代码时,我们会遇到以下问题:
System.Security.Cryptography.CryptographicException:系统找不到指定的文件。
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)
代码类似于这样:
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
有一些资源试图通过声明你应该给用户读取机器密钥存储的访问权限来回答这个问题,但是没有确定性的解决方案可以解决这个问题。
环境:IIS 6.0,Windows Server 2003 R2,.NET 3.5 SP1