RemoteCertificateValidationCallback Delegado

Definición

Comprueba el certificado remoto de capa de sockets seguros (SSL) que se usa para la autenticación.

public delegate bool RemoteCertificateValidationCallback(System::Object ^ sender, X509Certificate ^ certificate, X509Chain ^ chain, SslPolicyErrors sslPolicyErrors);
public delegate bool RemoteCertificateValidationCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors);
type RemoteCertificateValidationCallback = delegate of obj * X509Certificate * X509Chain * SslPolicyErrors -> bool
Public Delegate Function RemoteCertificateValidationCallback(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean 

Parámetros

sender
Object

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

certificate
X509Certificate

Certificado que se usa para autenticar la entidad remota.

chain
X509Chain

Cadena de entidades de certificación asociadas al certificado remoto.

sslPolicyErrors
SslPolicyErrors

Uno o varios errores asociados al certificado remoto.

Valor devuelto

Valor Boolean que determina si se acepta el certificado especificado para la 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.


// 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.

// 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 delegado contiene los errores de sslPolicyErrors 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

Nombre Description
GetMethodInfo(Delegate)

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

Se aplica a

Consulte también