C#获取ssl服务器证书的绑定
我正在运行一个自托管的web服务。通信是通过https进行的,我使用的是自签名服务器认证。端口是可变的。因此,在每个应用程序启动时,我都会在存储中查找证书,并将其绑定到特定端口。这是我用netsh和delete/add做的 是否有方法确定应用程序启动之前是否已执行命令“http add sslcert ipport=0.0.0.0:{port}”?因此,我不必在每次应用程序启动时都删除和添加它。 我的想法是调用“http show sslcert ipport=0.0.0.0:{port}”并查看输出。但这不太好。。 那么有没有一种C#方法可以做到这一点C#获取ssl服务器证书的绑定,c#,ssl-certificate,self-hosting,x509certificate2,netsh,C#,Ssl Certificate,Self Hosting,X509certificate2,Netsh,我正在运行一个自托管的web服务。通信是通过https进行的,我使用的是自签名服务器认证。端口是可变的。因此,在每个应用程序启动时,我都会在存储中查找证书,并将其绑定到特定端口。这是我用netsh和delete/add做的 是否有方法确定应用程序启动之前是否已执行命令“http add sslcert ipport=0.0.0.0:{port}”?因此,我不必在每次应用程序启动时都删除和添加它。 我的想法是调用“http show sslcert ipport=0.0.0.0:{port}”并查
public static bool ActivateCertificate(int port, X509Certificate2 certificate, string appId)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
bool certificateExists = false;
store.Open(OpenFlags.ReadWrite);
try
{
foreach (var cert in store.Certificates)
{
if (cert.Thumbprint != null && cert.Thumbprint.Equals(certificate.Thumbprint, StringComparison.InvariantCultureIgnoreCase))
{
certificateExists = true;
break;
}
}
if (!certificateExists)
{
store.Add(certificate);
}
}
catch (Exception ex)
{
Log.Error(ex, "Could not activate certificate!");
return false;
}
finally
{
store.Close();
}
StringBuilder str = new StringBuilder();
ProcessStartInfo psi = new ProcessStartInfo() {CreateNoWindow = true, UseShellExecute = false, RedirectStandardOutput = true};
psi.FileName = "netsh";
psi.Arguments = $"http show sslcert ipport=0.0.0.0:{port}";
Process procShow = Process.Start(psi);
while (procShow != null && !procShow.StandardOutput.EndOfStream)
{
str.Append(procShow.StandardOutput.ReadLine());
}
Log.Warn(str.ToString);
// delete IPV4.
psi.Arguments = $"http delete sslcert ipport=0.0.0.0:{port}";
Process procDel = Process.Start(psi);
//exitCode = procDel.ExitCode;
while (procDel != null && !procDel.StandardOutput.EndOfStream)
{
str.Append(procDel.StandardOutput.ReadLine());
}
Log.Warn(str.ToString);
procDel?.WaitForExit(1000);
// IPV4-Adresse hinzufügen.
psi.Arguments = $"http add sslcert ipport=0.0.0.0:{port} certhash={certificate.Thumbprint.ToLower()} appid={{{appId}}}";
Process proc = Process.Start(psi);
//exitCode = proc.ExitCode;
while (proc != null && !proc.StandardOutput.EndOfStream)
{
str.Append(proc.StandardOutput.ReadLine());
}
/*
// delete IPV6
Log.Warn(str.ToString);
proc?.WaitForExit(1000);
psi.Arguments = $"http delete sslcert ipport=[::]:{port}";
Process procDelV6 = Process.Start(psi);
while (procDelV6 != null && !procDelV6.StandardOutput.EndOfStream)
{
str.Append(procDelV6.StandardOutput.ReadLine());
}
Log.Warn(str.ToString);
procDelV6?.WaitForExit(1000);
// IPV6-Adresse hinzufügen.
psi.Arguments = $"http add sslcert ipport=[::]:{port} certhash={certificate.Thumbprint.ToLower()} appid={{{appId}}}";
Process procV6 = Process.Start(psi);
while (procV6 != null && !procV6.StandardOutput.EndOfStream)
{
str.Append(procV6.StandardOutput.ReadLine());
}
Log.Warn(str.ToString);
procV6?.WaitForExit();
*/
return true;
}
不幸的是,我认为普遍接受的方法是按您的方式调用netsh。您可以查看,但这取决于安装的IIS 在我看来,执行
netsh http delete sslcert…
然后执行netsh http add sslcert…
是完全有效的。当绑定不存在时,delete
将失败,但这没关系