Skip to content

Commit

Permalink
Fixed virtual directory set password related.
Browse files Browse the repository at this point in the history
  • Loading branch information
lextm committed Oct 5, 2019
1 parent 85acc64 commit 324dada
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 55 deletions.
6 changes: 5 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ indent_size = 2
[*.sh]
end_of_line = lf
[*.{cmd, bat}]
end_of_line = crlf
end_of_line = crlf
[*.cs]

# IDE0067: Dispose objects before losing scope
dotnet_diagnostic.IDE0067.severity = error
67 changes: 47 additions & 20 deletions Microsoft.Web.Administration/IisExpressServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand All @@ -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()
Expand All @@ -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)
{
Expand Down
62 changes: 48 additions & 14 deletions Microsoft.Web.Administration/IisServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ internal override bool GetSiteState(Site site)
return false;
}

var process = new Process
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
Expand All @@ -78,7 +78,7 @@ internal override bool GetPoolState(ApplicationPool pool)
return false;
}

var process = new Process
using var process = new Process
{
StartInfo = new ProcessStartInfo
{
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -206,10 +216,34 @@ internal override IEnumerable<string> 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());
}
}
}
}
21 changes: 1 addition & 20 deletions Microsoft.Web.Administration/ServerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 324dada

Please sign in to comment.