2011-10-22 52 views
6

をapp.configを暗号化した後、私はapplicationSettingsの下で生きているセクションに渡して自分のアプリケーションの冒頭で次のメソッドを実行します。認識できない属性「configProtectionProvider」

public static void EncryptConfigSection(string sectionKey) 
    { 
     Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 
     ConfigurationSection section = config.GetSection(sectionKey); 
     if (section != null) 
     { 
      if (!section.SectionInformation.IsProtected) 
      { 
       if (!section.ElementInformation.IsLocked) 
       { 
        section.SectionInformation.ProtectSection("RSAProtectedConfigurationProvider"); 
        section.SectionInformation.ForceSave = true; 
        config.Save(ConfigurationSaveMode.Full); 
        ConfigurationManager.RefreshSection(sectionKey); 
       } 
      } 
     } 
    } 

は、ここではapp.config内のセクションの例です:

Unrecognized attribute 'configProtectionProvider'

:私はセクションからの設定のいずれかにアクセスしようとすると

<applicationSettings> 
    <Example.Properties.Settings> 
    <setting name="Key" serializeAs="String"> 
     <value>Value</value> 
    </setting> 
    </Example.Properties.Settings> 
</applicationSettings> 

、私は次のエラーを受け取ります3210

これは、デスクトップアプリケーションで、起動時にいくつかの設定を暗号化し、終了時に復号化する必要があります。

誰にもこの問題の解決策がありますか?

答えて

2

私は暗号化/アプリケーションの実行中に設定ファイルを復号化し、継続することができませんでした値を読み取る。

私が望んでいたものではありませんが、問題の解決策は、アプリケーションが実行される前にまず.configファイルを暗号化/復号化することでした。あなたはセクションを取得するためにConfigurationManager.GetSectionの静的なバージョンを使用することはできません - 私は1つの重要な注意点で、リック・スコットの答えの作業を取得するために管理 Encrypting Passwords in a .NET app.config File

3

このブログの記事によると、修正プログラムが親にRefreshSection()を呼び出すことです:

RefreshSection("applicationSettings") 

Unrecognized attribute configProtectionProvider

+0

VBと2017リックで修正が必要な、残念ながらこれは動作しませんでした。理由は分かりません。私はいくつかの異なる方法を試しました。動作する唯一のものは、設定を暗号化した後にアプリケーションを起動する場合です。 –

+0

@GrandMasterT 2年遅れましたが、私はリックの答えを得ることができました - 詳細については私の答えを見てください:)また、右の場所に私を導くリック+1。 –

3

:ここ

は、私はしませんでした別のアプローチであるが、面白そうでしたリフレッシュ後に - 代わりにConfiguration.GetSectionを使用する必要があります。

全コード:

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

if (!section.SectionInformation.IsProtected) 
{ 
    // Encrypt the section. 
    section.SectionInformation.ProtectSection("DPAPIProtection"); 
    section.SectionInformation.ForceSave = true; 
    config.Save(ConfigurationSaveMode.Modified); 

    // The passwords are now encrypted. 
    // Refresh the *parent* of the section that your passwords are in. 
    ConfigurationManager.RefreshSection("configuration"); 

    // Now use Configuration.GetManager to retrieve the new password section. 
    // This doesn't throw the Configuration Exception :) 
    ConfigurationSection section2 = config.GetSection("passwordSection") as ConfigurationSection; 
} 
3

私はこれが見つかりました:http://andybrennan.wordpress.com/2014/06/05/unrecognized-attribute-configprotectionprovider-after-encrypting-app-config/を。それが問題を解決します。

ブログに書かれてちょうどこのメソッドを使用します。

private void ResetConfigMechanism() 
{ 
    typeof(ConfigurationManager) 
     .GetField("s_initState", BindingFlags.NonPublic | 
           BindingFlags.Static) 
     .SetValue(null, 0); 

    typeof(ConfigurationManager) 
     .GetField("s_configSystem", BindingFlags.NonPublic | 
            BindingFlags.Static) 
     .SetValue(null, null); 

    typeof(ConfigurationManager) 
     .Assembly.GetTypes() 
     .Where(x => x.FullName == 
        "System.Configuration.ClientConfigPaths") 
     .First() 
     .GetField("s_current", BindingFlags.NonPublic | 
           BindingFlags.Static) 
     .SetValue(null, null); 
} 

は、設定を更新/保存した後、それを呼び出します。

+0

しかし、このソリューションは非常にうまく動作し、問題を解決した唯一の解決策でした(いくつかのプロジェクトでインクルードファイルとsettings.settingsファイルを使用した複雑な設定スキーマ)。この理由のためにアップヴォートを与えました。 私は自分のコードでその使用方法を文書化し、変更するたびに再テストする必要があることに注意しました。NET Frameworkのバージョン、それが動作することを確認し、マイクロソフトが同じことをするための公的な方法を実装しているかどうかチェックアウトする。 –

0

はこのポストはちょうど私の一日保存し、念のため、誰かが

Private Sub ResetConfigMechanism() 
     GetType(ConfigurationManager).GetField("s_initState", BindingFlags.NonPublic Or BindingFlags.Static).SetValue(Nothing, 0) 
     GetType(ConfigurationManager).GetField("s_configSystem", BindingFlags.NonPublic Or BindingFlags.Static).SetValue(Nothing, Nothing) 
     GetType(ConfigurationManager).Assembly.GetTypes().Where(Function(x) x.FullName = "System.Configuration.ClientConfigPaths").First().GetField("s_current", BindingFlags.NonPublic Or BindingFlags.Static).SetValue(Nothing, Nothing) 
    End Sub 
関連する問題