RemoteCertificateValidationCallback Delegasikan
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Memverifikasi sertifikat Secure Sockets Layer (SSL) jarak jauh yang digunakan untuk autentikasi.
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);
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
Parameter
- sender
- Object
Objek yang berisi informasi status untuk validasi ini.
- certificate
- X509Certificate
Sertifikat yang digunakan untuk mengautentikasi pihak jarak jauh.
- chain
- X509Chain
Rantai otoritas sertifikat yang terkait dengan sertifikat jarak jauh.
- sslPolicyErrors
- SslPolicyErrors
Satu atau beberapa kesalahan yang terkait dengan sertifikat jarak jauh.
Tampilkan Nilai
Boolean Nilai yang menentukan apakah sertifikat yang ditentukan diterima untuk autentikasi.
Contoh
Contoh kode berikut mengimplementasikan metode yang dipanggil oleh instans RemoteCertificateValidationCallback kelas. Jika ada kesalahan validasi, metode ini menampilkannya dan mengembalikan false, yang mencegah komunikasi dengan server yang tidak diaauthenticated.
// 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;
}
Contoh kode berikut membuat delegasi menggunakan metode yang ditentukan dalam contoh kode sebelumnya.
// 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;
}
Keterangan
Argumen delegasi sslPolicyErrors berisi kesalahan sertifikat yang dikembalikan oleh SSPI saat mengautentikasi klien atau server. Nilai Boolean yang dikembalikan oleh metode yang dipanggil oleh delegasi ini menentukan apakah autentikasi diizinkan untuk berhasil.
Delegasi ini digunakan dengan SslStream kelas .
Metode Ekstensi
| Nama | Deskripsi |
|---|---|
| GetMethodInfo(Delegate) |
Mendapatkan objek yang mewakili metode yang diwakili oleh delegasi yang ditentukan. |