简体   繁体   English

SSLStream和委托的一般用法

[英]SSLStream and general use of delegates

I've never used delegates before but understand the basic concept after I've been struggling for the past 2 hours. 在过去2个小时的努力中,我从未使用过委托,但了解基本概念。 I've created a simple SSL Client/Server with mutual authentication. 我创建了一个具有相互身份验证的简单SSL客户端/服务器。 Now my actual problem: 现在我的实际问题是:

In the example a delegate is used to authenticate the server/client. 在该示例中 ,委托用于认证服务器/客户端。

SslStream sslStream = new SslStream(
            client.GetStream(), 
            false, 
            new RemoteCertificateValidationCallback (ValidateServerCertificate), 
            null
            );

The ValidateServerCertificate is the method which is run when the delegate is invoked. ValidateServerCertificate是调用委托时运行的方法。

public static bool ValidateServerCertificate(
          object sender,
          X509Certificate certificate,
          X509Chain chain,
          SslPolicyErrors sslPolicyErrors)
    {
       if (sslPolicyErrors == SslPolicyErrors.None)
            return true;

        Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

        // Do not allow this client to communicate with unauthenticated servers.
        return false;
    }

The thing that confuses me more than anything is how in the hell the ValidateServerCertificate is getting the parameters. 让我感到困惑的是,ValidateServerCertificate在到底如何获取参数。 Maybe this is a stupid question and you are laughing right now but please bear with me. 也许这是一个愚蠢的问题,您现在正在笑,但是请忍受我。 I've been sitting in front of tutorials and explanations for the past hours and I've found nothing which helped me. 在过去的几个小时中,我一直坐在教程和解释的前面,但没有发现任何对我有帮助的东西。

Thanks in advance 提前致谢

The SslStream class, will at some point in time, invoke the delegate. SslStream类将在某个时间点调用委托。 When it invokes the delegate it will be forced to provide values for the parameters that the delegate is expecting. 当它调用委托时,它将被迫为委托期望的参数提供值。 All of the methods that the delegate represents are then called, using those parameters. 然后使用这些参数调用委托代表的所有方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM