Files
Umbraco-CMS/src/Umbraco.Web/Install/InstallSteps/ConfigureMachineKey.cs

64 lines
2.4 KiB
C#
Raw Normal View History

2017-09-13 17:35:20 +02:00
using System.Linq;
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>
public override Task<InstallSetupResult> ExecuteAsync(bool? model)
2017-09-08 19:39:13 +02: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);
// 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();
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);
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
}