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;
}
}
看看https://solutiondesign.com/blog/-/blogs/a-simple-trick-to-centralize-your-net-configurati-1/ – Ravikumar