考虑到http://msdn.microsoft.com/en-us/library/ms145065%28v=vs.110%29.aspx方法,第二个参数clientCertificateRequired
如果将其设置为true,则需要一个客户端证书。如果不是,它将抛出异常。客户端证书将在属性RemoteCertificate中可用。
如果设置为false,则不需要客户端证书,属性RemoteCertificate将始终为空。即使一个是由客户提供。
我喜欢做的是让让客户端决定是否会提供证书。但是,如果他们确实提供了一个,我想知道它在服务器上。
我首先尝试将变量设置为true,如果失败,则回退到不需要证书。但是,这将导致“已通过身份验证的异常”。
try{
sslStream.AuthenticateAsServer(x509certificate, true, SslProtocols.Tls, true);
}catch(Exception ex){
sslStream.AuthenticateAsServer(x509certificate, false, SslProtocols.Tls, true);
}
发布于 2015-05-22 20:21:55
我坚信这是一个文档缺陷。
实际上,参数clientCertificateRequired
将控制客户端证书是否是而不是忽略。这意味着:
clientCertificateRequired = false
将忽略服务器端的任何客户端证书。不检查证书是否存在或是否有效。
clientCertificateRequired = true
将尊重服务器端发送的任何客户端证书。如果缺少客户端证书,则使用SslPolicyErrors.RemoteCertificateNotAvailable
调用验证回调,这将导致在使用默认实现时捕获的异常。
因此,在您的示例中:将clientCertificateRequired
设置为true
并实现自定义验证回调,如下所示:
var client = server.AcceptTcpClient()
var networkStream = client.GetStream()
var sslStream = new SslStream(
networkStream,
false,
(sender, certificate, chain, errors) =>
{
if (errors == SslPolicyErrors.None)
{
return true;
}
if (errors == SslPolicyErrors.RemoteCertificateNotAvailable)
{
// seems to be okay for you
return true;
}
return false;
},
(sender, host, certificates, certificate, issuers) => x509certificate
);
sslStream.AuthenticateAsServer(x509certificate, true, SslProtocols.Tls, true);
https://stackoverflow.com/questions/26930398
复制相似问题