使用 Advanced Installer,我尝试创建一个自定义操作,在安装时加密连接字符串。
我好像不能在这里使用“~”。 (我将我的工作代码从 MVC 项目移到了这里)。
该行是否有一个简单的替代方法,或者我是否被迫完全重写并使用例如使用某种 Stream 的解决方案(像这样 Modifying Web.Config During Installation
自定义操作抛出的异常: System.Reflection.TargetInvocationException:异常已被抛出 调用的目标。 ---> 系统.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;
}
}
最佳答案
路径问题的解决方案是使用带有一些映射的长 ConfigurationManager
,而不是网络版本 WebConfigurationManager
。
var map = new ExeConfigurationFileMap { ExeConfigFilename = path };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
加密与代码一样工作正常,但是保存的问题仍然没有解决,因为执行时间太早了。安装尚未完成,web.config 尚未复制到 APPDIR
。
关于c# - 如何在高级安装程序 (13.3) 自定义操作中加密连接字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45592251/