winform selfhost 创建的webapi自建https证书
1、打开PowerShell,输入以下命令,该命令会创建一个证书,并颁发给localhost
New-SelfSignedCertificate -certstorelocation cert:\localmachine\my -dnsname localhost -NotAfter 2060-01-01
打开IIS,可以看得到
2、修改证书密码aa123
$pwd = ConvertTo-SecureString -String "aa123" -Force -AsPlainText
3、导出证书,标红色部门为证书指纹,根据实际情况修改,双击第一步详情可以看得到,或第一步创建完后可以看得到
Export-PfxCertificate -cert cert:\localMachine\my\A95FFC1C6EDEAB10AFE62A59219F28D02D8EB45B -FilePath d:cert.pfx -Password $pwd
4、输入命令
netsh
5、拿到该证书的指纹并监听ip以及端口
http add sslcert ipport=0.0.0.0:19191 certhash=A95FFC1C6EDEAB10AFE62A59219F28D02D8EB45B appid={41992502-E5D4-4794-BB01-D4A7414480CC}
6、winform里面的启动的API记得改为https
var config = new HttpSelfHostConfiguration("https://localhost:19191"); config.EnableCors();
查看绑定端口:netsh http show sslcert
删除绑定端口:netsh http delete sslcert ipport=0.0.0.0:443
参考:
https://learn.microsoft.com/en-us/powershell/module/pki/new-selfsignedcertificate?view=windowsserver2022-ps
https://www.cnblogs.com/scoluo/p/10190570.html
https://blog.csdn.net/martian665/article/details/139649456