【问题标题】:Programmatically import and bind SSL cert以编程方式导入和绑定 SSL 证书
【发布时间】:2020-02-24 15:57:32
【问题描述】:

我正在努力将 SSL 证书支持添加到 VB.Net API,但我在通过代码使其工作时遇到了问题。如果我手动将证书导入 IIS,然后运行我的 netsh 命令,我可以让它工作。我有一个使用mkcert 创建的证书,并将其导入 IIS。

  1. 将证书导入 IIS
  2. cmd:netsh http delete sslcert ipport=0.0.0.0:443
  3. 使用 https/443 baseurl 启动我的 API
  4. 这按预期工作。

当我尝试通过代码执行此操作时,它不起作用,我收到 netsh 错误a specified logon session does not exist。这是我用来导入和绑定证书的代码。

' Import the certificate into IIS LocalComputer, this is needed for the ip address bindings.
Dim IIS = New X509Store(StoreName.My, StoreLocation.LocalMachine)
IIS.Open(OpenFlags.ReadWrite)
Try
    For Each certificate As X509Certificate2 In x509
        IIS.Add(certificate)
    Next
Finally
    IIS.Close()
End Try

' Bind the SSL Cert to the network address and port.
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
Dim cmd As String = " /K " + "netsh http add sslcert ipport=0.0.0.0:" + port.ToString() + " certhash=" + CertificateThumbprint + " appid={myAppId}"
Console.WriteLine(cmd)
pi.Arguments = cmd
pi.FileName = "cmd.exe"
p.StartInfo = pi
p.Start()

【问题讨论】:

标签: vb.net iis-7 ssl-certificate iis-7.5 netsh


【解决方案1】:

所以我可以通过更改以下内容来完成这项工作:

Try
    Dim certificate = New X509Certificate2(CertificatePath, CertificatePassword, X509KeyStorageFlags.Exportable Or X509KeyStorageFlags.MachineKeySet)
    IIS.Open(OpenFlags.ReadWrite)
    IIS.Add(certificate)
Finally
    IIS.Close()
End Try

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 2012-09-29
    • 2018-12-23
    • 2016-05-20
    • 2011-09-11
    相关资源
    最近更新 更多