首页 文章

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

提问于
浏览
5

在我们的程序安装过程中,我们运行此方法来加密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);
        }
    }
}

到目前为止工作正常 . 但是,如果我运行此程序,我们遇到几台计算机出现以下错误“无法使用提供程序_345249解密 . 来自提供程序的错误消息: The RSA key container could not be opened ” .

当然我搜索并找到this help,但这不起作用 . 有任何想法吗?

3 回答

  • 0

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

  • 3

    是 .

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

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

    UPDATE

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

  • 4

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

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

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

相关问题