Leer en inglés

Compartir vía


RemoteCertificateValidationCallback Delegado

Definición

Comprueba el certificado SSL (Secure Sockets Layer) remoto que se utiliza para la autenticación.

C#
public delegate bool RemoteCertificateValidationCallback(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors);
C#
public delegate bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors);

Parámetros

sender
Object

Objeto que contiene información de estado para esta validación.

certificate
X509Certificate

Certificado utilizado para autenticar la parte remota.

chain
X509Chain

La cadena de entidades de certificación asociadas al certificado remoto.

sslPolicyErrors
SslPolicyErrors

Uno o varios errores asociados al certificado remoto.

Valor devuelto

Un valor Boolean que determina si el certificado especificado es aceptado para autenticación.

Ejemplos

En el ejemplo de código siguiente se implementa un método invocado por una instancia de la RemoteCertificateValidationCallback clase . Si hay errores de validación, este método los muestra y devuelve false, lo que impide la comunicación con el servidor no autenticado.

C#

// The following method is invoked by the RemoteCertificateValidationDelegate.
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;
}

En el ejemplo de código siguiente se crea el delegado mediante el método definido en el ejemplo de código anterior.

C#
// Create a TCP/IP client socket.
// machineName is the host running the server application.
TcpClient client = new TcpClient(machineName,5000);
Console.WriteLine("Client connected.");
// Create an SSL stream that will close the client's stream.
SslStream sslStream = new SslStream(
    client.GetStream(),
    false,
    new RemoteCertificateValidationCallback (ValidateServerCertificate),
    null
    );
// The server name must match the name on the server certificate.
try
{
    sslStream.AuthenticateAsClient(serverName);
}
catch (AuthenticationException e)
{
    Console.WriteLine("Exception: {0}", e.Message);
    if (e.InnerException != null)
    {
        Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
    }
    Console.WriteLine ("Authentication failed - closing the connection.");
    client.Close();
    return;
}

Comentarios

El argumento del sslPolicyErrors delegado contiene los errores de certificado devueltos por SSPI al autenticar el cliente o el servidor. El Boolean valor devuelto por el método invocado por este delegado determina si la autenticación puede realizarse correctamente.

Este delegado se usa con la SslStream clase .

Métodos de extensión

GetMethodInfo(Delegate)

Obtiene un objeto que representa el método representado por el delegado especificado.

Se aplica a

Producto Versiones
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

Consulte también