I have this code that works, that takes the 4 properties and make a connectionString element in the web.config and save it encrypted. But it only works on a hardcoded path.
How do I solve this, when I want to create or use a web.config that comes from AI? I've tryed creating a web.config in "Files and Folders", but my Custom Action looks for that file before it's in the APPDIR. The Custom Action Execution Time is set to Immediately and placed in Install Execution Stage between "Add Resources" and "Finish Execution" . I've also try'd a little with temp. files but with no luck.
I'm wondering if there is an easy fix, or I should try to delay my Custom Action to first run after the Installation Wizard is done, or maybe use a temp web.config file, I could modify and save to my APPDIR. Or maybe there are another simple path I should use at Installation time instead of APPDIR?
Code: Select all
[CustomAction]
public static ActionResult EncryptConnStr(Session session)
{
try
{
var connStr = BuildConnStr(session["CONN_STR_SERVER"], session["CONN_STR_DATABASE"], session["CONN_STR_USERNAME"], session["CONN_STR_PASSWORD"]);
var path = Path.Combine(@"C:\Users\radbyx\Documents", "web.config"); // <--- TODO Hardcoded path.
var map = new ExeConfigurationFileMap { ExeConfigFilename = path };
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
ConnectionStringsSection section = (ConnectionStringsSection)config.GetSection("connectionStrings");
// Add Connectionstrings element
section.ConnectionStrings.Add(new ConnectionStringSettings(GetConnectionStringName(), connStr));
// Encrypt
section.SectionInformation.ProtectSection(ConnStrEncryptionKey);
// Save the configuration file.
config.Save(ConfigurationSaveMode.Modified, true);
return ActionResult.Success;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "Trace: " + ex.StackTrace, ex.Message);
throw;
}
}