2017-09-13 17:35:20 +02:00
|
|
|
|
using System.Linq;
|
2019-01-11 14:30:04 +11:00
|
|
|
|
using System.Threading.Tasks;
|
2017-09-08 19:39:13 +02:00
|
|
|
|
using System.Web.Configuration;
|
|
|
|
|
|
using System.Xml.Linq;
|
|
|
|
|
|
using Umbraco.Core.IO;
|
|
|
|
|
|
using Umbraco.Core.Security;
|
|
|
|
|
|
using Umbraco.Web.Install.Models;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Install.InstallSteps
|
|
|
|
|
|
{
|
|
|
|
|
|
[InstallSetupStep(InstallationType.NewInstall,
|
|
|
|
|
|
"ConfigureMachineKey", "machinekey", 2,
|
|
|
|
|
|
"Updating some security settings...",
|
|
|
|
|
|
PerformsAppRestart = true)]
|
|
|
|
|
|
internal class ConfigureMachineKey : InstallSetupStep<bool?>
|
|
|
|
|
|
{
|
2017-09-13 17:35:20 +02:00
|
|
|
|
public override string View => HasMachineKey() == false ? base.View : "";
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Don't display the view or execute if a machine key already exists
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
2017-09-13 17:35:20 +02:00
|
|
|
|
private static bool HasMachineKey()
|
2017-09-08 19:39:13 +02:00
|
|
|
|
{
|
2017-09-13 17:35:20 +02:00
|
|
|
|
var section = (MachineKeySection) WebConfigurationManager.GetSection("system.web/machineKey");
|
2017-09-08 19:39:13 +02:00
|
|
|
|
return section.ElementInformation.Source != null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The step execution method
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
|
/// <returns></returns>
|
2019-01-11 14:30:04 +11:00
|
|
|
|
public override Task<InstallSetupResult> ExecuteAsync(bool? model)
|
2017-09-08 19:39:13 +02:00
|
|
|
|
{
|
2019-01-28 19:21:59 +01:00
|
|
|
|
if (model.HasValue && model.Value == false) return Task.FromResult<InstallSetupResult>(null);
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
|
|
|
|
|
//install the machine key
|
2017-09-13 17:35:20 +02:00
|
|
|
|
var fileName = IOHelper.MapPath($"{SystemDirectories.Root}/web.config");
|
2017-09-08 19:39:13 +02:00
|
|
|
|
var xml = XDocument.Load(fileName, LoadOptions.PreserveWhitespace);
|
|
|
|
|
|
|
2019-10-26 13:02:19 +01:00
|
|
|
|
// we only want to get the element that is under the root, (there may be more under <location> tags we don't want them)
|
|
|
|
|
|
var systemWeb = xml.Root.Element("system.web");
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
|
|
|
|
|
// Update appSetting if it exists, or else create a new appSetting for the given key and value
|
|
|
|
|
|
var machineKey = systemWeb.Descendants("machineKey").FirstOrDefault();
|
2019-01-28 19:21:59 +01:00
|
|
|
|
if (machineKey != null) return Task.FromResult<InstallSetupResult>(null);
|
2017-09-08 19:39:13 +02:00
|
|
|
|
|
|
|
|
|
|
var generator = new MachineKeyGenerator();
|
|
|
|
|
|
var generatedSection = generator.GenerateConfigurationBlock();
|
|
|
|
|
|
systemWeb.Add(XElement.Parse(generatedSection));
|
|
|
|
|
|
|
|
|
|
|
|
xml.Save(fileName, SaveOptions.DisableFormatting);
|
|
|
|
|
|
|
2019-01-11 14:30:04 +11:00
|
|
|
|
return Task.FromResult<InstallSetupResult>(null);
|
2017-09-08 19:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public override bool RequiresExecution(bool? model)
|
|
|
|
|
|
{
|
|
|
|
|
|
return HasMachineKey() == false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-09-23 10:08:18 +02:00
|
|
|
|
}
|