3

使用高級安裝程序時,我試圖進行自定義操作,即在安裝時加密連接字符串。如何在高級安裝程序中對連接字符串進行加密(13.3)自定義操作

我好像不能在這裏使用「〜」。 (我把我的工作代碼從MVC項目移到這裏)。

是否有一個簡單的替代方案,或者我不得不完全重寫和使用使用流的somekind的溶液(如本Modifying Web.Config During Installation

異常由自定義動作拋出: System.Reflection.TargetInvocationException:異常已被調用的 目標拋出---> System.ArgumentException: 該應用程序相對虛擬路徑「〜」這裏不允許使用

自定義操作:

[CustomAction] 
public static ActionResult EncryptConnStr(Session session) 
{ 
    try 
    { 
     var config = WebConfigurationManager.OpenWebConfiguration("~"); 
     var section = (ConnectionStringsSection)config.GetSection("connectionStrings"); 
     var cms = section.ConnectionStrings[GetConnectionStringName()]; 
     var connStr = BuildConnStr(session["CONN_STR_SERVER"], session["CONN_STR_DATABASE"], session["CONN_STR_USERNAME"], session["CONN_STR_PASSWORD"]); 

     if (cms == null) 
     { 
      // Add new Connection String 
      section.ConnectionStrings.Add(new ConnectionStringSettings(GetConnectionStringName(), connStr)); 
     } 
     else 
     { 
      // Update existing Connection String 
      cms.ConnectionString = connStr; 
     } 

     // Encrypt 
     section.SectionInformation.ProtectSection(ConnStrEncryptionKey); 

     // Save the configuration file. 
     config.Save(ConfigurationSaveMode.Modified); 

     return ActionResult.Success; 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show(ex.StackTrace, ex.Message); 
     throw; 
    } 
} 
+0

看看https://solutiondesign.com/blog/-/blogs/a-simple-trick-to-centralize-your-net-configurati-1/ – Ravikumar

回答

0

的解決路徑問題,是使用ConfigurationManager長一些映射,像這樣,而不是web版本WebConfigurationManager。作爲代碼,但仍然沒有解決與保存的問題,因爲執行時間是早

var map = new ExeConfigurationFileMap { ExeConfigFilename = path }; 
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 

加密工作正常。安裝未完成,並且web.config尚未被複制到APPDIR

相關問題