From 324dadac6b7c9940feca9e364696e211c479d26c Mon Sep 17 00:00:00 2001 From: Lex Li Date: Sat, 5 Oct 2019 15:38:01 -0400 Subject: [PATCH] Fixed virtual directory set password related. --- .editorconfig | 6 +- .../IisExpressServerManager.cs | 67 +++++++++++++------ .../IisServerManager.cs | 62 +++++++++++++---- Microsoft.Web.Administration/ServerManager.cs | 21 +----- 4 files changed, 101 insertions(+), 55 deletions(-) diff --git a/.editorconfig b/.editorconfig index ab3ba544..b9339a45 100644 --- a/.editorconfig +++ b/.editorconfig @@ -27,4 +27,8 @@ indent_size = 2 [*.sh] end_of_line = lf [*.{cmd, bat}] -end_of_line = crlf \ No newline at end of file +end_of_line = crlf +[*.cs] + +# IDE0067: Dispose objects before losing scope +dotnet_diagnostic.IDE0067.severity = error diff --git a/Microsoft.Web.Administration/IisExpressServerManager.cs b/Microsoft.Web.Administration/IisExpressServerManager.cs index 908c5d2f..6167c160 100644 --- a/Microsoft.Web.Administration/IisExpressServerManager.cs +++ b/Microsoft.Web.Administration/IisExpressServerManager.cs @@ -7,7 +7,6 @@ using System.ComponentModel; using System.Diagnostics; using System.IO; -using System.Linq; using System.Runtime.InteropServices; using System.Xml.Linq; using System.Xml.XPath; @@ -68,7 +67,7 @@ private static string GetPrimaryExecutable() return File.Exists(fileName) ? fileName : null; } - internal override string GetAppCmd() + internal override void SetPassword(VirtualDirectory virtualDirectory, string password) { var directory = Path.Combine( Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), @@ -81,12 +80,38 @@ internal override string GetAppCmd() "IIS Express"); if (!Directory.Exists(directory)) { - return null; + // IMPORTANT: fallback to default password setting. Should throw encryption exception. + virtualDirectory.Password = password; + return; } } - var fileName = Path.Combine(directory, "appcmd.exe"); - return File.Exists(fileName) ? fileName : null; + var appcmd = Path.Combine(directory, "appcmd.exe"); + if (!File.Exists(appcmd)) + { + // IMPORTANT: fallback to default password setting. Should throw encryption exception. + virtualDirectory.Password = password; + return; + } + + using var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = appcmd, + Arguments = $"set vdir /vdir.name:\"{virtualDirectory.LocationPath()}\" /password:{password} /apphostconfig:\"{FileName}\"", + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden, + Verb = "runas", + UseShellExecute = true + } + }; + process.Start(); + process.WaitForExit(); + if (process.ExitCode != 0) + { + throw new Exception(process.ExitCode.ToString()); + } } private Version GetIisExpressVersion() @@ -106,23 +131,25 @@ internal override bool GetSiteState(Site site) { try { - using (var process = new Process()) + using var process = new Process { - var start = process.StartInfo; - start.Verb = site.Bindings.ElevationRequired && !PublicNativeMethods.IsProcessElevated - ? "runas" - : null; - start.UseShellExecute = true; - start.FileName = "cmd"; - start.Arguments = - $"/c \"\"{CertificateInstallerLocator.FileName}\" /config:\"{site.FileContext.FileName}\" /siteId:{site.Id}\""; - start.CreateNoWindow = true; - start.WindowStyle = ProcessWindowStyle.Hidden; - process.Start(); - process.WaitForExit(); + StartInfo = new ProcessStartInfo + { + Verb = site.Bindings.ElevationRequired && !PublicNativeMethods.IsProcessElevated + ? "runas" + : null, + UseShellExecute = true, + FileName = "cmd", + Arguments = + $"/c \"\"{CertificateInstallerLocator.FileName}\" /config:\"{site.FileContext.FileName}\" /siteId:{site.Id}\"", + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden + } + }; + process.Start(); + process.WaitForExit(); - return process.ExitCode == 1; - } + return process.ExitCode == 1; } catch (Win32Exception ex) { diff --git a/Microsoft.Web.Administration/IisServerManager.cs b/Microsoft.Web.Administration/IisServerManager.cs index 5ddb65d0..20b2ef8f 100644 --- a/Microsoft.Web.Administration/IisServerManager.cs +++ b/Microsoft.Web.Administration/IisServerManager.cs @@ -52,7 +52,7 @@ internal override bool GetSiteState(Site site) return false; } - var process = new Process + using var process = new Process { StartInfo = new ProcessStartInfo { @@ -78,7 +78,7 @@ internal override bool GetPoolState(ApplicationPool pool) return false; } - var process = new Process + using var process = new Process { StartInfo = new ProcessStartInfo { @@ -101,14 +101,16 @@ internal override void Start(Site site) var appcmd = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "inetsrv", "appcmd.exe"); if (File.Exists(appcmd)) { - var process = new Process + using var process = new Process { StartInfo = new ProcessStartInfo { FileName = appcmd, Arguments = $"start site \"{site.Name}\"", CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden + WindowStyle = ProcessWindowStyle.Hidden, + Verb = "runas", + UseShellExecute = true } }; process.Start(); @@ -121,14 +123,16 @@ internal override void Stop(Site site) var appcmd = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "inetsrv", "appcmd.exe"); if (File.Exists(appcmd)) { - var process = new Process + using var process = new Process { StartInfo = new ProcessStartInfo { FileName = appcmd, Arguments = $"stop site \"{site.Name}\"", CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden + WindowStyle = ProcessWindowStyle.Hidden, + Verb = "runas", + UseShellExecute = true } }; process.Start(); @@ -141,14 +145,16 @@ internal override void Start(ApplicationPool pool) var appcmd = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "inetsrv", "appcmd.exe"); if (File.Exists(appcmd)) { - var process = new Process + using var process = new Process { StartInfo = new ProcessStartInfo { FileName = appcmd, Arguments = $"start apppool \"{pool.Name}\"", CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden + WindowStyle = ProcessWindowStyle.Hidden, + Verb = "runas", + UseShellExecute = true } }; process.Start(); @@ -161,14 +167,16 @@ internal override void Stop(ApplicationPool pool) var appcmd = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "inetsrv", "appcmd.exe"); if (File.Exists(appcmd)) { - var process = new Process + using var process = new Process { StartInfo = new ProcessStartInfo { FileName = appcmd, Arguments = $"stop apppool \"{pool.Name}\"", CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden + WindowStyle = ProcessWindowStyle.Hidden, + Verb = "runas", + UseShellExecute = true } }; process.Start(); @@ -181,14 +189,16 @@ internal override void Recycle(ApplicationPool pool) var appcmd = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "inetsrv", "appcmd.exe"); if (File.Exists(appcmd)) { - var process = new Process + using var process = new Process { StartInfo = new ProcessStartInfo { FileName = appcmd, Arguments = $"recycle apppool \"{pool.Name}\"", CreateNoWindow = true, - WindowStyle = ProcessWindowStyle.Hidden + WindowStyle = ProcessWindowStyle.Hidden, + Verb = "runas", + UseShellExecute = true } }; process.Start(); @@ -206,10 +216,34 @@ internal override IEnumerable GetSchemaFiles() return Directory.Exists(directory) ? Directory.GetFiles(directory) : base.GetSchemaFiles(); } - internal override string GetAppCmd() + internal override void SetPassword(VirtualDirectory virtualDirectory, string password) { var appcmd = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "inetsrv", "appcmd.exe"); - return File.Exists(appcmd) ? appcmd : null; + if (!File.Exists(appcmd)) + { + // IMPORTANT: fallback to default password setting. Should throw encryption exception. + virtualDirectory.Password = password; + return; + } + + using var process = new Process + { + StartInfo = new ProcessStartInfo + { + FileName = appcmd, + Arguments = $"set vdir /vdir.name:\"{virtualDirectory.LocationPath()}\" /password:{password}", + CreateNoWindow = true, + WindowStyle = ProcessWindowStyle.Hidden, + Verb = "runas", + UseShellExecute = true + } + }; + process.Start(); + process.WaitForExit(); + if (process.ExitCode != 0) + { + throw new Exception(process.ExitCode.ToString()); + } } } } diff --git a/Microsoft.Web.Administration/ServerManager.cs b/Microsoft.Web.Administration/ServerManager.cs index bbb78cbb..d843d288 100644 --- a/Microsoft.Web.Administration/ServerManager.cs +++ b/Microsoft.Web.Administration/ServerManager.cs @@ -83,27 +83,8 @@ internal virtual bool Verify(string path, string executable) return Directory.Exists(path.ExpandIisExpressEnvironmentVariables(executable)); } - internal void SetPassword(VirtualDirectory virtualDirectory, string password) + internal virtual void SetPassword(VirtualDirectory virtualDirectory, string password) { - var appcmd = GetAppCmd(); - if (appcmd == null) - { - // IMPORTANT: fallback to default password setting. Should throw encryption exception. - virtualDirectory.Password = password; - return; - } - - var process = Process.Start(appcmd, $"set vdir /vdir.name:\"{virtualDirectory.LocationPath()}\" /password:{password}"); - process.WaitForExit(); - if (process.ExitCode != 0) - { - throw new Exception(process.ExitCode.ToString()); - } - } - - internal virtual string GetAppCmd() - { - return null; } private void Initialize()