From 390857e06525941dcebb50a19c501d790baf7fb4 Mon Sep 17 00:00:00 2001 From: Gary Sharp Date: Wed, 25 Sep 2024 18:16:32 +1000 Subject: [PATCH] attempt to preserve IIS bindings so future installations can restore them --- Disco.Services/Disco.Services.csproj | 6 ++ .../Interop/IIS/PreserveIisBindingsTask.cs | 79 +++++++++++++++++++ Disco.sln | 4 + 3 files changed, 89 insertions(+) create mode 100644 Disco.Services/Interop/IIS/PreserveIisBindingsTask.cs diff --git a/Disco.Services/Disco.Services.csproj b/Disco.Services/Disco.Services.csproj index 78372818..7e22a1a9 100644 --- a/Disco.Services/Disco.Services.csproj +++ b/Disco.Services/Disco.Services.csproj @@ -62,6 +62,11 @@ ..\packages\Microsoft.Owin.Security.4.2.2\lib\net45\Microsoft.Owin.Security.dll + + False + ..\..\..\Installer\Microsoft.Web.Administration.dll + False + True ..\packages\Microsoft.Web.Infrastructure.1.0.0.0\lib\net40\Microsoft.Web.Infrastructure.dll @@ -371,6 +376,7 @@ + diff --git a/Disco.Services/Interop/IIS/PreserveIisBindingsTask.cs b/Disco.Services/Interop/IIS/PreserveIisBindingsTask.cs new file mode 100644 index 00000000..10a3465c --- /dev/null +++ b/Disco.Services/Interop/IIS/PreserveIisBindingsTask.cs @@ -0,0 +1,79 @@ +using Disco.Data.Repository; +using Disco.Services.Tasks; +using Microsoft.Web.Administration; +using Microsoft.Win32; +using Quartz; +using System; +using System.Linq; +using System.Text; + +namespace Disco.Services.Interop.DiscoServices +{ + public class PreserveIisBindingsTask : ScheduledTask + { + private const string DiscoRegistryKey = @"SOFTWARE\Disco"; + + public override string TaskName { get { return "Disco ICT - Preserve IIS Bindings"; } } + public override bool SingleInstanceTask { get { return true; } } + public override bool CancelInitiallySupported { get { return false; } } + + public override void InitalizeScheduledTask(DiscoDataContext Database) + { + TriggerBuilder triggerBuilder = TriggerBuilder.Create() + .StartAt(DateTimeOffset.Now.AddMinutes(10)); + + ScheduleTask(triggerBuilder); + } + + protected override void ExecuteTask() + { + // retrieve IIS bindings + var bindings = GetWebsiteBindings(); + + if (bindings == null || !bindings.Any()) + { + ScheduledTasksLog.LogScheduledTaskInformation(Status.TaskName, Status.SessionId, "Bindings for IIS Web Site 'Disco' not found"); + return; + } + + using (var key = Registry.LocalMachine.CreateSubKey(DiscoRegistryKey)) + { + key.SetValue("WebsiteBindings", bindings, RegistryValueKind.MultiString); + } + } + + private string[] GetWebsiteBindings() + { + using (var manager = new ServerManager()) + { + var site = manager.Sites["Disco"]; + + if (site == null) + return null; + + var bindings = site.Bindings; + var sb = new StringBuilder(); + var result = new string[bindings.Count]; + for (int i = 0; i < bindings.Count; i++) + { + var binding = bindings[i]; + sb.Append(binding.BindingInformation); + sb.Append(';'); + sb.Append(binding.Protocol); + if (string.Equals(binding.Protocol, "https", StringComparison.OrdinalIgnoreCase)) + { + sb.Append(';'); + sb.Append(Convert.ToBase64String(binding.CertificateHash)); + sb.Append(';'); + sb.Append(binding.CertificateStoreName); + } + + result[i] = sb.ToString(); + sb.Clear(); + } + + return result; + } + } + } +} \ No newline at end of file diff --git a/Disco.sln b/Disco.sln index b653e5b8..23aad660 100644 --- a/Disco.sln +++ b/Disco.sln @@ -16,6 +16,10 @@ EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Disco.Services", "Disco.Services\Disco.Services.csproj", "{B80A737F-BD6A-4986-9182-DD7B932BD950}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Disco.Web", "Disco.Web\Disco.Web.csproj", "{241F4F43-6ACB-482D-8CBF-8F4E4B4DB0FE}" + ProjectSection(ProjectDependencies) = postProject + {15BD9561-A3C7-4608-9F7E-F1A1CFB60055} = {15BD9561-A3C7-4608-9F7E-F1A1CFB60055} + {D6B85A86-0FAA-4B04-BC9E-D01A08C03387} = {D6B85A86-0FAA-4B04-BC9E-D01A08C03387} + EndProjectSection EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Disco.Client", "Disco.Client\Disco.Client.csproj", "{D6B85A86-0FAA-4B04-BC9E-D01A08C03387}" EndProject