2016-09-30 9 views
0

Googleには、外部アプリの設定ファイルを共有する必要がある多くのアプリケーションとサービスがあります。外部ファイルには、暗号化する情報を含むconfigSectionが含まれています。 各サービスとアプリケーションはそれぞれのアプリケーションフォルダにあり、そこから問題が拡大し始めます。 App.configでは、外部ファイルは 'configSource'属性または 'file'属性で参照できます。外部設定ファイルがappフォルダまたはappサブフォルダに存在しないため、 'configSource'は使用できません。それでは、 'file'属性を使用する必要があります。以下のように設定の外部アプリの設定と暗号化

<customSettings file=”path to setting”/> 

'customSettings' configSectionが定義されています

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

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

私はだから:私は、このようなコードを使用してconfigSectionを暗号化しようとしている

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

ファイル属性を使用して(私は疑う)。 configセクションはApp.configで暗号化されており、外部ファイルは暗号化されていません。 App.configで暗号化される内容は、ちょうど

<customSettings file=”path to setting”/> 

です。かなり役に立たないです。

これは、個々のアプリケーションとサービスが外部設定ファイルを暗号化できないことを意味します。 私は、外部の設定ファイルと同じディレクトリに小さなアプリケーションを置くという考えがありました。このアプリケーションの目的は、代わりに 'configSource'属性を使用して外部の設定ファイルを暗号化することでした。このアプローチはまったく機能しません。何も起こらず、何も暗号化されません。

もう少し調べるために、私はAppSetに 'customSettings'を置き、セクションを正常に暗号化しました。その後、暗号化されたデータを外部ファイルにコピーして、外部設定ファイルで暗号化が機能するかどうかをテストしました。これは 'configSource'では正常に動作しますが、 'file'属性を使用すると例外がスローされます。

例外がスローさ:我々はapp.configをで「ファイル」属性を使用しなければならないので

Unrecognized attribute 'configProtectionProvider' 

が、私は今、2つの問題があります。

  1. 外部ファイルを暗号化できません。
  2. 外部ファイルを手動で暗号化すると、 'file'属性を使用して外部ファイルを読み取ることができません。
+0

私は最初の問題を解決しました。 それは私が電話する必要があったことが判明: 'section.SectionInformation.ForceSave = true;' 実際に設定を保存する前に。 configSource属性を使用すると、外部設定ファイルが暗号化されるようになりました –

答えて

0

を読んで多くの研究の後、私はクラスがあれば、file="file path"属性によって指さconfigSectionのを解決することができなかったことに気づいNameValueFileSectionHandlerのコードに探して外部のconfigSectionは暗号化されています。これがバグかどうかは、NameValueFileSectionHandlerには分かりません。たぶん誰かがそれに答えることができます。

しかし私はNameValueCollectionを返し、暗号化された外部設定ファイルを扱うことができる自分自身のNameValueFileSectionHandlerを書きました。

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; 
    } 
} 

ハンドラはデフォルト設定の暗号化に制限されています。しかし、app.configファイルで定義されているカスタムプロバイダをサポートするためにCreate関数を拡張することは可能であると想像できます。

1

カスタム設定セクションを作成し、appsettingsセクションを使用せずにカスタム設定セクションに依存することもできます。もちろん、外部ファイルを読み込む独自のハンドラを作成する必要があります。その時点のファイルを指すことができ、ファイル全体が難読化され、XML標準に準拠しない可能性があります。

または、暗号化されたファイルを指すアプリの設定で、手動でファイルに

+0

あなたが作成したポイントをお寄せいただきありがとうございます。自分のハンドラを作成しました。しかし、それはそうでした、それは 'NameValueFileSectionHandler'がどのようにそれを行うかのインラインでした。したがって、.NET System.Configurationの名前空間ですでに利用可能なものと多くの相乗効果を得ることができます。上記の私のソリューションを参照してください。 –

関連する問題