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

标签 c# configuration encryption rsa

在我们的程序安装过程中,我们运行此方法来加密 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 key 容器”。。

当然我搜索并找到了this help ,但这不起作用。有什么想法吗?

最佳答案

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

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

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

关于c# - 使用 RsaProtectedConfigurationProvider 加密/解密 app.config 部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4256569/

相关文章:

.net - 我什么时候应该将配置数据存储在数据库中而不是 web.config 中?

hibernate - 如何在Grails 3中将属性传递给Enver?

java - 决定加密算法

c# - 如何使用箭头键在文本框之间移动光标?

c# - 每次按键仅允许 1 个值

c# - 具有乘法表数学运算值的组合框

amazon-web-services - 在 Amazon AMI (Centos) 上使用 Let's Encrypt - 甚至无法安装包,有什么想法吗?

c# - 我应如何解决此正则表达式方法以仅验证MM/DD/YYYY的日期?

c# - 如何更改 WCF 的默认配置?

C#简单加密