2016-09-30 38 views
0

我們有一些需要共享外部應用程序配置文件的應用程序和服務。外部文件包含一個configSection,其中包含我們要加密的信息。 每個服務和應用程序駐留在它自己的應用程序文件夾中,這就是問題開始升級的地方。 在App.config中,可以使用'configSource'或'file'屬性來引用外部文件。 'configSource'無法使用,因爲外部配置文件不在應用程序文件夾或應用程序子文件夾中。因此我們必須使用'文件'屬性。外部應用程序配置和設置加密

<customSettings file=」path to setting」/> 

爲隨後的 'customSettings' configSection被定義爲:

<configSections> 
    <section name="customSettings" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> 
</configSections> 

我則嘗試使用這樣的代碼來加密configSection:

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
ConfigurationSection section = config.GetSection("customSettings"); 

if (!section.SectionInformation.IsProtected) 
{ 
    section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); 
    config.Save(); 
} 

因爲我使用文件屬性(我懷疑)。配置部分在App.config中加密,而外部文件不是。在App.config中加密的只是

<customSettings file=」path to setting」/> 

。這是很沒用的。

這意味着個人應用程序和服務無法加密外部配置文件。 然後,我有了一個想法,即將一個小應用程序放置在與外部配置文件相同的目錄中。此應用程序的目的是通過使用'configSource'屬性來加密外部配置文件。這種方法根本不起作用。什麼也沒有發生,沒有任何加密。

爲了進一步研究,我將'customSettings'放​​在App.config中併成功加密了該部分。然後,我將加密的數據複製到外部文件,以測試加密是否可以在外部配置文件中工作。這對'configSource'很好,但在使用'file'屬性時引發異常。

拋出異常:

Unrecognized attribute 'configProtectionProvider' 

因爲我們必須使用的app.config的「文件」屬性我現在有2個問題。

  1. 無法加密外部文件。
  2. 如果手動加密外部文件,我無法使用'文件'屬性讀取它。
+0

我已經解決了我的第一個問題。 原來,我需要調用: 'section.SectionInformation.ForceSave = true;' 在實際保存配置之前。當使用configSource屬性時,外部配置文件現在被加密 –

回答

0

經過大量研究並查看代碼NameValueFileSectionHandler的代碼後,我意識到該類無法解析file="file path"屬性指向的configSection,if加密的外部configSection。不知道這是否是一個錯誤或不在NameValueFileSectionHandler。也許這裏有人可以回答。

但是我最終寫了我自己的NameValueFileSectionHandler,它可能會返回NameValueCollection並處理加密的外部配置文件。

public class NameValueFileProtectedSectionHandler : IConfigurationSectionHandler 
{ 
    public object Create(object parent, object configContext, XmlNode section) 
    { 
     object result = parent; 

     XmlNode fileAttribute = section.Attributes.RemoveNamedItem("file"); 

     if (fileAttribute == null && fileAttribute.Value.Length == 0) 
     { 
      return new NameValueSectionHandler().Create(result, null, section); 
     } 

     IConfigErrorInfo configXmlNode = fileAttribute as IConfigErrorInfo; 

     if (configXmlNode == null) 
     { 
      return null; 
     } 

     string directory = Path.GetDirectoryName(configXmlNode.Filename); 
     string absoluteFilePath = Path.GetFullPath(directory + fileAttribute.Value); 

     if (!File.Exists(absoluteFilePath)) 
     { 
      throw new ConfigurationErrorsException(string.Format("external config file: {0} does not exists", absoluteFilePath)); 
     } 

     var configXmlDocument = new ConfigXmlDocument(); 
     try 
     { 
      configXmlDocument.Load(absoluteFilePath); 
     } 
     catch (XmlException e) 
     { 
      throw new ConfigurationErrorsException(e.Message, e, absoluteFilePath, e.LineNumber); 
     } 

     if (section.Name != configXmlDocument.DocumentElement.Name) 
     { 
      throw new ConfigurationErrorsException(string.Format("Section name '{0}' in app.config does not match section name '{1}' in file '{2}'", section.Name, configXmlDocument.DocumentElement.Name, absoluteFilePath)); 
     } 

     var nodeToDecrypt = configXmlDocument.DocumentElement["EncryptedData"]; 

     if (nodeToDecrypt == null) 
     { 
      throw new ConfigurationErrorsException(string.Format("External encrypted file {0} does not contain EncryptedData element", absoluteFilePath)); 
     } 

     var protectionProvider = new DpapiProtectedConfigurationProvider(); 
     var decryptedConfigSection = protectionProvider.Decrypt(nodeToDecrypt); 

     result = new NameValueSectionHandler().Create(result, null, decryptedConfigSection); 

     return result; 
    } 
} 

處理程序被限制爲默認配置加密。但我可以想象,可以擴展Create函數以支持app.config文件中定義的自定義提供程序。

1

您可以選擇的選項是創建自定義配置節,避免使用appsettings節並依賴自定義配置節。您需要創建自己的處理程序來讀取外部文件。當然,您可以指向任何文件,整個文件可能會被混淆,並且不符合XML標準

或者您可以添加設置在指向加密文件並在文件中手動讀取的應用程序設置中

+0

感謝您提供的優點。創建了我自己的處理程序。但是這樣做,它與NameValueFileSectionHandler是如何內聯的。因此,與.NET System.Configuration名稱空間中已有的功能具有協同作用。請參閱上面的解決方案 –

相關問題