当服务器要求客户端使用 SSL 证书连接时,请使用 TsgcWebSocketClient 的 TLSOptions 属性来设置证书文件。
通过 SChannel 连接需要将 TLSOptions.IOHandler 设置为 iohSChannel。
SChannel 支持两种证书认证类型:
1. 使用 PFX 证书
2. 设置 Windows 系统中已安装证书的哈希证书。
PFX 证书是包含证书和私钥的文件,有时您的证书为 PEM 格式,使用前必须将其转换为 PFX 格式。
使用以下 openssl 命令将 PEM 证书转换为 PFX
openssl pkcs12 -inkey certificate-pem.key -in certificate-pem.crt -export -out certificate.pfx
一旦证书为 PFX 格式,您只需部署证书并将 TLSOptions.CertFile 属性设置为其路径即可。
TLSOptions.IOHandler = iohSChannel
TLSOptions.CertFile = <certificate path>
TLSOptions.Password = <certificate optional password>
如果证书已安装在 Windows 证书存储中,您只需知道证书指纹并在 TLSOptions.SChannel_Options 属性中设置即可。
在 PowerShell 中查找证书的哈希值非常简单,只需在证书容器上运行 dir 命令即可。
dir cert:\localmachine\my
哈希是十六进制的Thumbprint(指纹)值。
Directory: Microsoft.PowerShell.Security\Certificate::localmachine\my
Thumbprint Subject
---------- -------
C12A8FC8AE668F866B48F23E753C93D357E9BE10 CN=*.mydomain.com
获得指纹值后,您必须在 TLSOptions.SChannel_Options 属性中设置哈希值和证书位置。
TLSOptions.IOHandler = iohSChannel
TLSOptions.SChannel_Options.CertHash = <certificate thumbprint>
TLSOptions.SChannel_Options.CertStoreName = <certificate store name>
TLSOptions.SChannel_Options.CertStorePath = <certificate store path>
TLSOptions.Password = <certificate optional password>