Ich versuche, einen Netsh-Befehl auszuführen, der System.Process verwendet, das ein Argument übergibt, und ich bekomme einen "Der Parameter ist falsch." Rückkehr.Ungültige Zeichen, die mit Process.StandardInput.WriteLine übergeben wurden
Der Parameter informiert ist:
http hinzufügen sslcert ipport = 0.0.0.0: {port} certhash = {} CertificateHash appid = "{{00000000-0000-0000-0000-AABBCCDDEEFF}} ';
Dabei ist Port der ausgewählte Port und certificateHash der Hash des angegebenen Zertifikats.
wurde mir klar, dass die Rückkehr des Befehls zusätzliche versteckte Charaktere hat (??) für den Parameter certhash wie dieses Beispiel:
C: \ Windows \ System32> netsh http hinzufügen sslcert ipport = 0.0.0.0: 8787 certhash = ?? BDBCA9543D50108B6F43AA44852CD1D0F4C07B7C appid = '{00000000-0000-0000-0000-AABBCCDDEEFF}' Der Parameter ist falsch.
Gibt es eine Möglichkeit, die Zeichenfolge in dem Argument zu erzwingen, genau die, die ich übergebe? Fehle ich etwas Umwandlung?
Hier ist der Code ich benutze:
public static void RegisterCertificateToSslPort(string subjectName)
{
var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly);
var certificate = store.Certificates.Find(X509FindType.FindBySubjectName, subjectName, true);
var certificateHash = CleanThumbprint(certificate[0]?.GetCertHashString());
var result = CommandExecuter.AddSslCertificateToPort(certificateHash, "8787");
}
public static string CleanThumbprint(string mmcThumbprint)
{
return new string(mmcThumbprint.Where(c => char.IsLetter(c) || char.IsDigit(c)).ToArray());
}
private static string Execute(string command)
{
var startInfo =
new System.Diagnostics.ProcessStartInfo
{
WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden,
FileName = "cmd.exe",
Verb = "runas",
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
WorkingDirectory = @"C:\Windows\system32"
};
var standardOutput = new StringBuilder();
using (var process = System.Diagnostics.Process.Start(startInfo))
{
using (var sw = process?.StandardInput)
{
if (sw != null && sw.BaseStream.CanWrite)
{
sw.WriteLine(command);
}
}
while (process != null && !process.HasExited)
{
standardOutput.Append(process.StandardOutput.ReadToEnd());
}
standardOutput.Append(process?.StandardOutput.ReadToEnd());
}
return standardOutput.ToString();
}
public static string AddSslCertificateToPort(string certificateHash, string port, string appId = null)
{
var command =
$"netsh http add sslcert ipport=0.0.0.0:{port} certhash={certificateHash} appid='{{00000000-0000-0000-0000-AABBCCDDEEFF}}'";
return Execute(command);
}