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

相关问题