C# 解决无法识别的属性 configProtectionProvider
在使用.Net自身提供的加密本配置文件后再用System.Configuration.ConfigurationManager.AppSettings["key"]获取值时会出现“无法识别的属性 configProtectionProvider参考”
- 如果你是自定义加密值再保存到配置文件和则Aspnet_regiis.exe不会出现此问题,即不使用.Net默认的加密方式
- 使用.Net默认加密方式示例
Configuration configuration =ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
#region 保存配置文件
try
{
//加密配置信息
if(isProtected &&!configuration.AppSettings.SectionInformation.IsProtected)
{
configuration.AppSettings.SectionInformation.ForceSave=true;
configuration.AppSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
configuration.Save();
}
catch(Exception)
{- 使用此加密方式会加密节点下所有的数据
- .Net自身的加密是跟电脑相关的,即在开发者电脑上生成的配置文件发布到生产机器上(包括所有非开始者电脑)都无法获取配置值会出现以下问题
/// <summary>
/// 获取配置文件指定的值
/// </summary>
/// <param name="key">健</param>
/// <returns>健值</returns>
public static string GetConfig(string key)
{
//如果使用.Net对配置文件进行加密过,则访问ConfigurationManager.AppSettings会产生错误"无法识别的属性 configProtectionProvider"
if (string.IsNullOrEmpty(key)||ConfigurationManager.AppSettings[key]==null) return string.Empty;
return ConfigurationManager.AppSettings[key];
}
/// <summary>
/// 获取配置文件指定的值
/// </summary>
/// <param name="key">健</param>
/// <returns>健值</returns>
public static string GetConfig(string key)
{
if (string.IsNullOrEmpty(key)) return string.Empty;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (config.AppSettings.Settings[key] == null)
return string.Empty;
else
return config.AppSettings.Settings[key].Value;
}
/******************************************************************
* 创建人:HTL
* 创建时间:2013-2-21 16:58:54
* 说明:配置文件操作类(Winform,Asp.net)
* Email:huangyuan413026@163.com
*******************************************************************/
using System;
using System.Configuration;
namespace HTL
{
public sealed class ConfigHelp
{
#region appSettings节点
/// <summary>
/// 获取配置文件指定的值
/// </summary>
/// <param name="key">健</param>
/// <param name="defaultvalue">值为Null时返回的默认值</param>
/// <returns>健对应的值,如果为Null返回默认值</returns>
public static string GetConfig(string key, string defaultvalue)
{
string _value = GetConfig(key);
return string.IsNullOrEmpty(_value) ? defaultvalue : _value;
}
/// <summary>
/// 获取配置文件指定的值
/// </summary>
/// <param name="key">健</param>
/// <returns>健对应的值</returns>
public static string GetConfig(string key)
{
if (string.IsNullOrEmpty(key)) return string.Empty;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
try
{
if (config.AppSettings.Settings[key] == null)
return string.Empty;
else
return config.AppSettings.Settings[key].Value;
}
catch (Exception e)
{
throw e;
}
}
/// <summary>
/// appSettings节点下是否存在某健
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static bool IsExistKey(string key)
{
if (string.IsNullOrEmpty(key)) return false;
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
return config.AppSettings.Settings[key] != null;
} /// <summary>
/// 对appSettings节点添加健值
/// 如果健已经存在则更改值
/// 添加后重新保存并刷新该节点
/// </summary>
/// <param name="key">添加的健</param>
/// <param name="key">添加的值</param>
public static void AddConfig(string key, string value)
{
if (string.IsNullOrEmpty(key)) return;
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
try
{
if (IsExistKey(key))
configuration.AppSettings.Settings[key].Value = value;
else
configuration.AppSettings.Settings.Add(key, value); configuration.Save();
}
catch (Exception)
{
throw;
}
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// 对appSettings节点添加健值,一次添加或更改多个健值
/// 如果健已经存在则更改值
/// 添加后重新保存并刷新该节点
/// 默认不加密该appSettings节点数据
/// </summary>
/// <param name="dict">添加的健值集合</param>
public static void AddConfig(System.Collections.Generic.Dictionary<string, string> dict)
{
AddConfig(dict, false);
}
/// <summary>
/// 对appSettings节点添加健值
/// 如果健已经存在则更改值
/// 添加后重新保存并刷新该节点
/// 加密后的配置节不能通过ConfigurationManager.AppSettings[key]进行访问,否则会产生错误"无法识别的属性 configProtectionProvider"
/// 可以通过Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings[key]进行访问
/// </summary>
/// <param name="dict">添加的健值集合</param>
/// <param name="isProtected">是否加密appSettings节点数据,如果为TrueappSettings节点下所有数据都会被加密</param>
public static void AddConfig(System.Collections.Generic.Dictionary<string, string> dict, bool isProtected)
{
if (dict == null || dict.Count <= ) return;
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
#region //循环添加或更改健值
foreach (System.Collections.Generic.KeyValuePair<string, string> key_value in dict)
{
if (string.IsNullOrEmpty(key_value.Key)) continue;
if (IsExistKey(key_value.Key))
configuration.AppSettings.Settings[key_value.Key].Value = key_value.Value;
else
configuration.AppSettings.Settings.Add(key_value.Key, key_value.Value);
}//end foreach
#endregion
#region 保存配置文件
try
{
//加密配置信息
if (isProtected && !configuration.AppSettings.SectionInformation.IsProtected)
{
configuration.AppSettings.SectionInformation.ForceSave = true; configuration.AppSettings.SectionInformation.ProtectSection("DataProtectionConfigurationProvider");
}
configuration.Save(); }
catch (Exception)
{
throw;
}
ConfigurationManager.RefreshSection("appSettings");
ConfigurationManager.RefreshSection("configuration");
#endregion
}
/// <summary>
/// 删除AppSettings下指定的Name
/// </summary>
/// <param name="key">要删除的Name</param>
/// <returns></returns>
public static bool Remove(string key)
{
if (!IsExistKey(key)) return false;
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
configuration.AppSettings.Settings.Remove(key.Trim());
try
{
configuration.Save();
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch (Exception)
{
throw;
}
}
#endregion
#region connectionStrings节点
/// <summary>
/// ConnectionStrings.Count
/// </summary>
/// <returns></returns>
public static int GetConnsCount
{
get { return ConfigurationManager.ConnectionStrings.Count; }
}
/// <summary>
/// 读取数据库配置文件(connectionStrings节点)
/// </summary>
/// <param name="key">健名</param>
/// <returns></returns>
public static string GetConnConfig(string key)
{
return !string.IsNullOrEmpty(key) && ConfigurationManager.ConnectionStrings[key] != null ? ConfigurationManager.ConnectionStrings[key].ConnectionString : string.Empty;
}
/// <summary>
/// 健不存在或值为Null
/// </summary>
/// <param name="key">健名</param>
/// <returns></returns>
public static bool GetConfigConnIsNull(string key)
{
return string.IsNullOrEmpty(key) || ConfigurationManager.ConnectionStrings[key] == null || string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[key].ConnectionString);
}
/// <summary>
/// 保存配置数据库连接字符串
/// 如果不存在连接字符串,则创建并设置字符串
/// </summary>
/// <param name="key">要操作的节点</param>
/// <param name="value">值</param>
public static void AddConnConfig(string key, string value)
{
if (string.IsNullOrEmpty(key)) return;
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
if (configuration.ConnectionStrings.ConnectionStrings[key] == null)
configuration.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(key, value));
else
{
foreach (ConnectionStringSettings conn in configuration.ConnectionStrings.ConnectionStrings)
{
if (conn.Name != key || conn.ConnectionString == value) continue;
conn.ConnectionString = value;
break;
}
}
try
{
configuration.Save();
}
catch (Exception)
{
throw;
}
ConfigurationManager.RefreshSection("connectionStrings");
}
#endregion
}
}
C# 解决无法识别的属性 configProtectionProvider的更多相关文章
- 无法识别的属性 configProtectionProvider的解决方案
用RsaProtectedConfigurationProvider加密数据库连接字符串时,只要App.config有任何改动,都会提示无法识别的属性 configProtectionProvider ...
- 无法识别的属性“targetFramework”的解决方法
本文导读:网站发布后,在IIS中浏览的时候出现以下异常:无法识别的属性“targetFramework”,请注意属性名称区分大小写.出现这个问题是由IIS配置该站点的.NET Framework 版本 ...
- C# 无法识别的属性“targetFramework”。请注意属性名称区分大小写。错误解决办法
“/CRM”应用程序中的服务器错误. 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消息: 无法识别的属性“targetF ...
- IIS7.0提示---无法识别的属性“targetFramework”。请注意属性名称区分大小写。
当我把我做的网站放在IIS7.0的服务器上的时候,浏览时提示这个错误信息 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息并适当地修改配置文件. 分析器错误消 ...
- 分析器错误消息: 无法识别的属性“targetFramework”。请注意属性名称区分大小写。
转自:http://blog.sina.com.cn/s/blog_48964b12010157p0.html 配置错误说明: 在处理向该请求提供服务所需的配置文件时出错.请检查下面的特定错误详细信息 ...
- 无法识别的属性“targetFramework”
问题描述:无法识别的属性“targetFramework”.请注意属性名称区分大小写. 解决办法:修改.NET Framework 版本为相应版本即可,例如2.0换成4.0. 参考:http://bl ...
- 无法识别的属性“targetFramework”。请注意,属性名是大写和小写。错误的解决方案
"/CRM"应用server错. 配置错误 说明: 在处理向该请求提供服务所需的配置文件时出错.请检查以下的特定错误具体信息并适当地改动配置文件. 分析器错误消息: 无法识别的属性 ...
- selenium中webdriver识别class属性多个值中有空格的解决方案
初学自动化测试,貌似大家十有八九都是用百度网站进行练手的,特此感谢百度. http://www.baidu.com 页面中主要就是搜索框和提交按钮: 输入框各元素属性:<input id=&qu ...
- IIS发布网站,访问时出现无法识别的属性“targetFramework”错误
今天在IIS发布网站后,访问时出现无识别的属性“targetFramework”错误 错误描述: 错误原因: 是由IIS配置该站点的.NET Framework 版本与程序中的.NET Framewo ...
随机推荐
- Linux的经典shell命令整理
Linux的经典shell命令整理 1.删除0字节文件find -type f -size 0 -exec rm -rf {} \; 2.查看进程按内存从大到小排列ps -e -o “%C : %p ...
- HDU 6071 Lazy Running (同余最短路 dij)
Lazy Running Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)To ...
- JSTL-2
流程控制标签:if标签, choose标签, when标签, otherwise标签 <c:if>:的两种语法 1.<c:if test="" var=&qu ...
- (bc 1002)hdu 6016 count the sheep
Count the Sheep Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- pm2常用的命令
exit //退出 ssh www@25.17.1.54 // 远程登录主机sudo su // 获得 root 权限 并且进入目录 /home/wwwpm2 list // 查看当前的列表 id 和 ...
- vmware12安装centos7系统详解
1.首先需要准备的工具有vmware12和contos7的系统. vmvare12下载地址: http://pan.baidu.com/s/1i5vH50D contos7我自己使用的为1511版本. ...
- 【BZOJ 4169】 4169: Lmc的游戏 (树形DP)
4169: Lmc的游戏 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 44 Solved: 25 Description RHL有一天看到lmc在 ...
- CentOS 报错cannot execute binary file
在安装软件过程中执行文件,报错cannot execute binary file 1.查看是否root用户登录,当前用户是否有可执行权限 2.ls -l 查看文件是否具有可执行权限 3.要使用对应的 ...
- [BZOJ4887][TJOI2017]可乐(DP+矩阵快速幂)
题目描述 加里敦星球的人们特别喜欢喝可乐.因而,他们的敌对星球研发出了一个可乐机器人,并且放在了加里敦星球的1号城市上.这个可乐机器人有三种行为: 停在原地,去下一个相邻的城市,自爆.它每一秒都会随机 ...
- 《深入理解Spark-核心思想与源码分析》(六)第六章计算引擎
RDD是Spark对各类数据计算模型的统一抽象,被用于迭代计算过程以及任务输出结果的缓存读写. 在所有MapReduce框架中,shuffle是连接map任务和reduce任务的桥梁.shuffle性 ...