C# 自定义应用程序配置文件 节加密怎么实现?

熬夜程序猴 2015-11-10 02:17:01
由于要使用到配置文件,本人在程序中实现了自定义应用程序配置文件,但是在使用加密时配置节时出现以下异常:
“"未能使用提供程序“RsaProtectedConfigurationProvider”加密节“DataTypeGroup/DataTypeSection”。提供程序返回错误消息: 对象已存在。”

自定义配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<!-- 自定义节 -->
<sectionGroup name="DataTypeGroup">
<section name ="DataTypeSection" type = "AppConfiguration.DataTypeSection, AppConfiguration"/>
</sectionGroup>

</configSections>

<DataTypeGroup>
<DataTypeSection>
<!--
<node key="SqlDataType_00" type="char" SqlSqlDbType="c"></node>
-->
<SqlNodes name="SqlDataType">
<add key="SqlDataType_00" type="char" SqlDbType="Char"/>
<add key="SqlDataType_01" type="nchar" SqlDbType="NChar"/>
<add key="SqlDataType_02" type="varchar" SqlDbType="VarChar"/>
<add key="SqlDataType_03" type="nvarchar" SqlDbType="NVarChar"/>
<add key="SqlDataType_04" type="text" SqlDbType="Text"/>
<add key="SqlDataType_05" type="ntext" SqlDbType="NText"/>
<add key="SqlDataType_06" type="bit" SqlDbType="Bit"/>
<add key="SqlDataType_07" type="binary" SqlDbType="Binary"/>
<add key="SqlDataType_08" type="tinyint" SqlDbType="TinyInt"/>
<add key="SqlDataType_09" type="smallint" SqlDbType="SmallInt"/>
<add key="SqlDataType_10" type="int" SqlDbType="Int"/>
<add key="SqlDataType_11" type="bigint" SqlDbType="BigInt"/>
<add key="SqlDataType_12" type="float" SqlDbType="Float"/>
<add key="SqlDataType_13" type="real" SqlDbType="Real"/>
<add key="SqlDataType_14" type="number" SqlDbType="Decimal"/>
<add key="SqlDataType_15" type="smallmoney" SqlDbType="SmallMoney"/>
<add key="SqlDataType_16" type="money" SqlDbType="Money"/>
<add key="SqlDataType_17" type="smalldatetime" SqlDbType="SmallDateTime"/>
<add key="SqlDataType_18" type="datetime" SqlDbType="DateTime"/>
<add key="SqlDataType_19" type="image" SqlDbType="Image"/>
</SqlNodes>
<OleNodes name="AccessDataType">
<add key="AccessDBDataType_00" type="Text" OleDbType="VarWChar"/>
<add key="AccessDBDataType_01" type="Memo" OleDbType="LongVarWChar"/>
<add key="AccessDBDataType_02" type="Byte" OleDbType="UnsignedTinyInt"/>
<add key="AccessDBDataType_03" type="Boolean" OleDbType="Boolean"/>
<add key="AccessDBDataType_04" type="Date/Time" OleDbType="Date"/>
<add key="AccessDBDataType_05" type="Currency" OleDbType="Numeric"/>
<add key="AccessDBDataType_06" type="Short" OleDbType="SmallInt"/>
<add key="AccessDBDataType_07" type="Long" OleDbType="Integer"/>
<add key="AccessDBDataType_08" type="Single" OleDbType="Single"/>
<add key="AccessDBDataType_09" type="Double" OleDbType="Double"/>
<add key="AccessDBDataType_10" type="Binary" OleDbType="Binary"/>
<add key="AccessDBDataType_11" type="OLE Object" OleDbType="LongVarBinary"/>
</OleNodes>
</DataTypeSection>
</DataTypeGroup>
</configuration>


加密代码如下:

public static void Protect(string sectionName)
{
try
{
string provider = "RsaProtectedConfigurationProvider";
string path = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection section = config.GetSection(sectionName);
if (null != section && !section.SectionInformation.IsProtected)
{
section.SectionInformation.ProtectSection(provider);
section.SectionInformation.ForceSave = true;

config.Save(ConfigurationSaveMode.Full);
ConfigurationManager.RefreshSection(sectionName);
}
}
catch (Exception ex)
{
throw ex;
}
}
...全文
193 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
熬夜程序猴 2015-11-12
  • 打赏
  • 举报
回复
引用 3 楼 xdashewan 的回复:
参照http://www.cnblogs.com/litianfei/archive/2007/07/18/822048.html http://www.cnblogs.com/colder/p/3273074.html
创建RSA的密钥容器后 无法通过xml使用“NetFrameworkConfigurationKey"名称进行导入(提示:正在从文件导入 RSA 密钥...对象已存在。失败!),只能导入为其他名字。百度之后全是复制粘贴的资料,郁闷
熬夜程序猴 2015-11-11
  • 打赏
  • 举报
回复
自己顶上去!!! 继续顶!!
xdashewan 2015-11-11
  • 打赏
  • 举报
回复
参照http://www.cnblogs.com/litianfei/archive/2007/07/18/822048.html http://www.cnblogs.com/colder/p/3273074.html
熬夜程序猴 2015-11-10
  • 打赏
  • 举报
回复
自己顶上去!!!

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧