RemoteCertificateValidationCallback Délégué
Définition
Important
Certaines informations portent sur la préversion du produit qui est susceptible d’être en grande partie modifiée avant sa publication. Microsoft exclut toute garantie, expresse ou implicite, concernant les informations fournies ici.
Vérifie le certificat SSL (Secure Sockets Layer) distant utilisé pour l’authentification.
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
Paramètres
- sender
- Object
Objet qui contient des informations d’état pour cette validation.
- certificate
- X509Certificate
Certificat utilisé pour authentifier le tiers distant.
- chain
- X509Chain
Chaîne d’autorités de certification associée au certificat distant.
- sslPolicyErrors
- SslPolicyErrors
Une ou plusieurs erreurs associées au certificat distant.
Valeur renvoyée
Valeur Boolean qui détermine si le certificat spécifié est accepté pour l’authentification.
Exemples
L’exemple de code suivant implémente une méthode appelée par une instance de la RemoteCertificateValidationCallback classe. S’il existe des erreurs de validation, cette méthode les affiche et retourne false, ce qui empêche la communication avec le serveur non authentifié.
// 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;
}
L’exemple de code suivant crée le délégué à l’aide de la méthode définie dans l’exemple de code précédent.
// 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;
}
Remarques
L’argument du délégué contient toutes les erreurs de certificat retournées par SSPI lors de sslPolicyErrors l’authentification du client ou du serveur. La Boolean valeur retournée par la méthode appelée par ce délégué détermine si l’authentification est autorisée à réussir.
Ce délégué est utilisé avec la SslStream classe.
Méthodes d’extension
| Nom | Description |
|---|---|
| GetMethodInfo(Delegate) |
Obtient un objet qui représente la méthode représentée par le délégué spécifié. |