2012-01-11 9 views
1

私はASP.NETアプリケーション用のインストーラを作成していますが、インストーラでユーザーからビルドしたいデータフィールド(接続文字列、smtpサーバーなど)があります。入力し、暗号化してWeb.configファイルに格納します。私が抱えている問題は、WebConfigurationManagerとConfigurationManagerの両方が既存の構成ファイルを処理するように設計されていることです。新しい設定ファイルを作成し、保存する前に暗号化するにはどうすればよいですか?C#Web.configファイルをプログラムで作成する

+0

だけでXMLファイルを作成します。 –

+0

xmlファイルで暗号化を取得できますか? –

+1

最小限のweb.configをリソースとしてインストーラに保存し、ファイルに保存してから続行します。 –

答えて

3

希望のフィールドが含まれていない基本設定ファイルから始め、次にXDocumentまたはWebConfigurationMangerを使用して設定情報を追加して暗号化することをお勧めします。

Source: Encrypting and decrypting sensitive data in your web.config files using Protected configuration - Part IV

コンテンツ包みコードがダウン:

private void EncryptConfig() 
{ 
    // Open the Web.config file. 
    Configuration config = 
     WebConfigurationManager.OpenWebConfiguration("~"); 
    // Get the connectionStrings section. 
    ConnectionStringsSection section = 
    config.GetSection("connectionStrings") as ConnectionStringsSection; 

    // Toggle encryption. 
    if (section.SectionInformation.IsProtected) 
    { 
     section.SectionInformation.UnprotectSection(); 
    } 
    else 
    { 
    if (!section.SectionInformation.IsLocked) 
    { 
     section.SectionInformation 
       .ProtectSection("RsaProtectedConfigurationProvider");    

     section.SectionInformation.ForceSave = true;    

     //Save changes to the Web.config file.  
     config.Save(ConfigurationSaveMode.Full);   } 
    } 
} 
+0

ありがとう、私はしばらく時間をとっていくつかの問題に遭遇しましたが、私はこのようなものを得ることができました。 –