如何加密的app.config? [英] How to encrypt app.config?

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

问题描述

创建WPF(C#)的app.config

 <?XML版本=1.0>?; 
<结构>
<&是connectionStrings GT;
<清/>
<添加名称=名称
的providerName =MySql.Data
的connectionString =服务器= .NET; UID =; PWD = H;数据库=;字符集= UTF8;允许零日期时间= TRUE; />
< /&是connectionStrings GT;
< /结构>



使用代码C#:

 配置配置= ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
ConnectionStringsSection构造= config.ConnectionStrings;
如果(conStr.SectionInformation.IsProtected!)
{
conStr.SectionInformation.ProtectSection(RSAProtectedConfigurationProvider);
conStr.SectionInformation.ForceSave = TRUE;
config.Save();
}
,否则
{
的foreach(在conStr.ConnectionStrings ConnectionStringSettings SS)
Console.WriteLine(SS);
Console.Read();
}



config.Save(); - 导致异常:




{无法加密的部分是connectionStrings使用提供商
'RsaProtectedConfigurationProvider'的。从
提供的错误信息:对象已存在.\r\\\
}



解决方案

检查 SectionInformation.ProtectSection方法



同时检查的这里


Create app.config in wpf (c#)

<?xml version="1.0"?>
<configuration>
  <connectionStrings>
    <clear />
    <add name="Name"
     providerName="MySql.Data"
     connectionString="Server=.net;Uid=;Pwd=H;Database=;charset=utf8;Allow Zero Datetime=true;" />
  </connectionStrings>
</configuration>

used code C#:

    Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    ConnectionStringsSection conStr = config.ConnectionStrings;
    if (!conStr.SectionInformation.IsProtected)
    {
        conStr.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider");
        conStr.SectionInformation.ForceSave = true;
        config.Save();
    }
    else
    {
        foreach (ConnectionStringSettings ss in conStr.ConnectionStrings)
            Console.WriteLine(ss);
        Console.Read();
    }

config.Save(); - causes exception:

{"Failed to encrypt the section 'connectionStrings' using provider 'RsaProtectedConfigurationProvider'. The error message from the provider: Object already exists .\r\n"}

解决方案

Check the SectionInformation.ProtectSection Method

also check here

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

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