web developer tips (41):用自颁发的证书连接到远程IIS服务器

原文地址:establishing a remote connection to a IIS server with self-issued certificate will require a certificate validation delegate

要么通过WMSvc 要么通过自己的脚本,当你尝试与远程服务器建立一个连接,没有提供受信任的证书,你需要提供证书验证的委托,用来检查不受信任的证书。
https://www.watch-life.net/visual-studio/establishing-a-remote-connection-to-a-iis-server-with-self-issued-certificate.html

这个委托的签名如下:
Namespace: System.Net.Security
Assembly: System (in System.dll)

public delegate bool RemoteCertificateValidationCallback(
Object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
)

于是,接受所有服务器证书,你需要设置一个ServicePointManager的回调来来验证一个服务器的证书,如下所示:

ServicePointManager.ServerCertificateValidationCallback = RemoteCertificateValidationCallbackFlag;

bool RemoteCertificateValidationCallbackCheck(
Object sender,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors sslPolicyErrors
)
{
return true;
}

关于这个“委托”可以参考MSDN文档:
1、RemoteCertificateValidationCallback Delegate
2、ServicePointManager.ServerCertificateValidationCallback Property

你可能也会喜欢以下文章:

发表评论

电子邮件地址不会被公开。 必填项已用*标注