Files
Umbraco-CMS/src/Umbraco.Web/Install/InstallSteps/ConfigureMachineKey.cs
Kevin Jump 3211054063 look for the system.web section under the root configuration node
use Element to get the root system.web. DescendantsAndSelf can return multiple
2019-11-05 11:52:48 +01:00

64 lines
2.4 KiB
C#

using System.Linq;
using System.Threading.Tasks;
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?>
{
public override string View => HasMachineKey() == false ? base.View : "";
/// <summary>
/// Don't display the view or execute if a machine key already exists
/// </summary>
/// <returns></returns>
private static bool HasMachineKey()
{
var section = (MachineKeySection) WebConfigurationManager.GetSection("system.web/machineKey");
return section.ElementInformation.Source != null;
}
/// <summary>
/// The step execution method
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public override Task<InstallSetupResult> ExecuteAsync(bool? model)
{
if (model.HasValue && model.Value == false) return Task.FromResult<InstallSetupResult>(null);
//install the machine key
var fileName = IOHelper.MapPath($"{SystemDirectories.Root}/web.config");
var xml = XDocument.Load(fileName, LoadOptions.PreserveWhitespace);
// 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");
// Update appSetting if it exists, or else create a new appSetting for the given key and value
var machineKey = systemWeb.Descendants("machineKey").FirstOrDefault();
if (machineKey != null) return Task.FromResult<InstallSetupResult>(null);
var generator = new MachineKeyGenerator();
var generatedSection = generator.GenerateConfigurationBlock();
systemWeb.Add(XElement.Parse(generatedSection));
xml.Save(fileName, SaveOptions.DisableFormatting);
return Task.FromResult<InstallSetupResult>(null);
}
public override bool RequiresExecution(bool? model)
{
return HasMachineKey() == false;
}
}
}