博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C# 解决无法识别的属性 configProtectionProvider
阅读量:4549 次
发布时间:2019-06-08

本文共 9422 字,大约阅读时间需要 31 分钟。

在使用.Net自身提供的加密本配置文件后再用System.Configuration.ConfigurationManager.AppSettings["key"]获取值时会出现“无法识别的属性 configProtectionProvider参考”

注意:
  1. 如果你是自定义加密值再保存到配置文件和则Aspnet_regiis.exe不会出现此问题,即不使用.Net默认的加密方式
  2. 使用.Net默认加密方式示例
  3. 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){

     

  4. 使用此加密方式会加密节点下所有的数据
  5. .Net自身的加密是跟电脑相关的,即在开发者电脑上生成的配置文件发布到生产机器上(包括所有非开始者电脑)都无法获取配置值会出现以下问题
   
  
在Google和stackoverflow上搜索都没有解决看来
只有自己动脑啦,经过一番摸索最终解决将自己的解决方案记录如下:
之前的方法:
/// /// 获取配置文件指定的值/// /// 健/// 
健值
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];}

 

改进后的方法(不直接使用 ConfigurationManager 的AppSettings属性而是打开配置文件再进行访问及读写操作 ):
/// /// 获取配置文件指定的值/// /// 健/// 
健值
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;}

 

自己写的ConfigHelp类: 
/****************************************************************** * 创建人: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节点        ///         /// 获取配置文件指定的值        ///         /// 健        /// 值为Null时返回的默认值        /// 
健对应的值,如果为Null返回默认值
public static string GetConfig(string key, string defaultvalue) { string _value = GetConfig(key); return string.IsNullOrEmpty(_value) ? defaultvalue : _value; } /// /// 获取配置文件指定的值 /// /// 健 ///
健对应的值
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; } } /// /// appSettings节点下是否存在某健 /// /// ///
public static bool IsExistKey(string key) { if (string.IsNullOrEmpty(key)) return false; Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); return config.AppSettings.Settings[key] != null; } /// /// 对appSettings节点添加健值 /// 如果健已经存在则更改值 /// 添加后重新保存并刷新该节点 /// /// 添加的健 /// 添加的值 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"); } /// /// 对appSettings节点添加健值,一次添加或更改多个健值 /// 如果健已经存在则更改值 /// 添加后重新保存并刷新该节点 /// 默认不加密该appSettings节点数据 /// /// 添加的健值集合 public static void AddConfig(System.Collections.Generic.Dictionary
dict) { AddConfig(dict, false); } ///
/// 对appSettings节点添加健值 /// 如果健已经存在则更改值 /// 添加后重新保存并刷新该节点 /// 加密后的配置节不能通过ConfigurationManager.AppSettings[key]进行访问,否则会产生错误"无法识别的属性 configProtectionProvider" /// 可以通过Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings[key]进行访问 /// ///
添加的健值集合 ///
是否加密appSettings节点数据,如果为TrueappSettings节点下所有数据都会被加密 public static void AddConfig(System.Collections.Generic.Dictionary
dict, bool isProtected) { if (dict == null || dict.Count <= 0) return; Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); #region //循环添加或更改健值 foreach (System.Collections.Generic.KeyValuePair
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 } ///
/// 删除AppSettings下指定的Name /// ///
要删除的Name ///
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节点 ///
/// ConnectionStrings.Count /// ///
public static int GetConnsCount { get { return ConfigurationManager.ConnectionStrings.Count; } } ///
/// 读取数据库配置文件(connectionStrings节点) /// ///
健名 ///
public static string GetConnConfig(string key) { return !string.IsNullOrEmpty(key) && ConfigurationManager.ConnectionStrings[key] != null ? ConfigurationManager.ConnectionStrings[key].ConnectionString : string.Empty; } ///
/// 健不存在或值为Null /// ///
健名 ///
public static bool GetConfigConnIsNull(string key) { return string.IsNullOrEmpty(key) || ConfigurationManager.ConnectionStrings[key] == null || string.IsNullOrEmpty(ConfigurationManager.ConnectionStrings[key].ConnectionString); } ///
/// 保存配置数据库连接字符串 /// 如果不存在连接字符串,则创建并设置字符串 /// ///
要操作的节点 ///
值 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 }}
View Code

 

MSDN加密配置文件:
 
 

转载于:https://www.cnblogs.com/huangtailang/p/3940011.html

你可能感兴趣的文章
magento在产品详细页面添加分享链接的方法
查看>>
手机连不上eclipse
查看>>
<p>1、查询端口号占用,根据端口查看进程信息</p>
查看>>
selenium+Python(生成html测试报告)
查看>>
Postman—使用数据文件
查看>>
Winform开发框架之字典管理模块的更新,附上最新2013年全国最新县及县以上行政区划代码sql脚本...
查看>>
Window Server 2008无法运行7z命令
查看>>
visualvm监控远程机器上的Java程序
查看>>
IN-子查询
查看>>
Maximum Depth of Binary Tree
查看>>
Apache服务器访问过慢分析及解决
查看>>
学习5_STM32--外设通信方式
查看>>
CSS 基础语法
查看>>
修改后台传的值
查看>>
暑假假期周进度报告(第二周)
查看>>
运行安装服务步骤
查看>>
RDS for MySQL 如何使用 Percona Toolkit
查看>>
System.map
查看>>
在Pandas中更改列的数据类型【方法总结】
查看>>
(原)lua使用ffi调用c程序的函数
查看>>