使用RsaProtectedConfigurationProvider加密/解密app.config部分 [英] Encryption / Decryption of app.config sections using RsaProtectedConfigurationProvider

查看:1115
本文介绍了使用RsaProtectedConfigurationProvider加密/解密app.config部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在安装我们的程序期间,我们运行此方法到app.config的encrpyt部分:

  //获取应用程序配置文件。 
配置config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

//定义Rsa提供程序名称。
const string provider =RsaProtectedConfigurationProvider;

//获取要保护的部分。
ConfigurationSection connStrings = config.ConnectionStrings;

if(connStrings!= null)
{
if(!connStrings.SectionInformation.IsProtected)
{
if(!connStrings.ElementInformation.IsLocked )
{
//保护部分。
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的部分。如果我没有运行命令行/控制台作为管理员,我会得到一个更加神秘的命令行错误:对象已经存在。


During the installation of our program we run this method to encrpyt sections of the 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);
        }
    }
}

Works fine so far. But if I run this program, we encounter of several machines the following error "Failed to decrypt using provider 'RsaProtectedConfigurationProvider'. Error message from the provider: The RSA key container could not be opened".

Of course I searched and found this help, but this doesn't work. Any ideas?

解决方案

I ran into similar issues while debugging within Visual Studio 2010 on Win 7 with UAC set to it's default protection.

In order for me to get around this issue, I had to run Visual Studio as the Administrator ("Run as Administrator").

I had the same issue with trying to run the aspnet_regiis.exe to encrypt the section of my web.config. If I didn't run the commandline/console "as Administrator" I would get a commandline error that was even more cryptic: "Object already exists."

这篇关于使用RsaProtectedConfigurationProvider加密/解密app.config部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆