使用RsaProtectedConfigurationProvider加密/解密app.config部分

在我们的程序安装过程中,我们运行此方法来加密app.config的各个部分:

// Get the application configuration file. Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); // Define the Rsa provider name. const string provider = "RsaProtectedConfigurationProvider"; // Get the section to protect. ConfigurationSection connStrings = config.ConnectionStrings; if (connStrings != null) { if (!connStrings.SectionInformation.IsProtected) { if (!connStrings.ElementInformation.IsLocked) { // Protect the section. connStrings.SectionInformation.ProtectSection(provider); connStrings.SectionInformation.ForceSave = true; config.Save(ConfigurationSaveMode.Full); } } } 

到目前为止工作正常。 但是如果我运行这个程序,我们会遇到几台机器出现以下错误“无法使用提供程序’RsaProtectedConfigurationProvider解密’。来自提供程序的错误消息: 无法打开RSA密钥容器 ”。

当然我搜索并找到了这个帮助 ,但这不起作用。 有任何想法吗?

我在Win 7上的Visual Studio 2010中进行调试时遇到了类似的问题,UAC设置为默认保护。

为了让我解决这个问题,我必须以管理员身份运行Visual Studio(“以管理员身份运行”)。

尝试运行aspnet_regiis.exe来加密我的web.config部分时遇到了同样的问题。 如果我没有“以管理员身份”运行命令行/控制台,我会得到一个更加神秘的命令行错误:“对象已经存在。”

是。

原因是那些工作的机器在他们的machine.config中设置了RsaProtectedConfigurationProvider 。 那些不工作,没有它 – 只需手动为这些机器添加它

我想这是aspnet_regiis.exe所做的一个步骤。 我无法想象你想在所有客户端机器上运行它。

UPDATE

好的,我已经在你的问题中以粗体显示了错误的主要部分 – 你说得对,这是一个不同的问题。 这是一个安全问题。 如果根据操作系统查看位置C:\ Documents and Settings \ All Users \ Application Data \ Microsoft \ Crypto \ RSA \ MachineKeysC:\ ProgramData \ Microsoft \ Crypto \ RSA \ MachineKeys ,您会看到许多文件。 您的进程可以访问该文件夹,因此只需授予文件访问整个文件夹的权限,以获取应用程序的标识或特定文件(时间戳将告诉您是否已创建它)。

我在app.config上得到了这个,该app.config运行在设置为SQL Server的Windows Server上。 它没有安装IIS。 machine.config文件列出了RSAProtectedConfigurationProvider作为默认值,但是当我们查看Aliostad上面提到的两个文件夹时,文件夹是空的。没有安装任何密钥。 我们使用aspnet_regiis工具来创建自定义键。 然后我们使用它来授予对批处理作业运行的标识的访问权限。 所有这些都是以管理员身份运行cmd.exe和aspnet_regiis。