Leer en inglés

Compartir a través de


LocalCertificateSelectionCallback Delegado

Definición

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

C#
public delegate System.Security.Cryptography.X509Certificates.X509Certificate? LocalCertificateSelectionCallback(object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate? remoteCertificate, string[] acceptableIssuers);
C#
public delegate System.Security.Cryptography.X509Certificates.X509Certificate LocalCertificateSelectionCallback(object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate? remoteCertificate, string[] acceptableIssuers);
C#
public delegate System.Security.Cryptography.X509Certificates.X509Certificate LocalCertificateSelectionCallback(object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers);

Parámetros

sender
Object

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

targetHost
String

Servidor host especificado por el cliente.

localCertificates
X509CertificateCollection

X509CertificateCollection que contiene los certificados locales.

remoteCertificate
X509Certificate

Certificado utilizado para autenticar la parte remota.

acceptableIssuers
String[]

Matriz de String de emisores de certificados aceptables para la parte remota.

Valor devuelto

X509Certificate utilizado para establecer una conexión SSL.

Ejemplos

En el ejemplo de código siguiente se muestra una implementación de método para este delegado.

C#
public static X509Certificate SelectLocalCertificate(
    object sender,
    string targetHost,
    X509CertificateCollection localCertificates,
    X509Certificate remoteCertificate,
    string[] acceptableIssuers)
{	
    Console.WriteLine("Client is selecting a local certificate.");
    if (acceptableIssuers != null &&
        acceptableIssuers.Length > 0 &&
        localCertificates != null &&
        localCertificates.Count > 0)
    {
        // Use the first certificate that is from an acceptable issuer.
        foreach (X509Certificate certificate in localCertificates)
        {
            string issuer = certificate.Issuer;
            if (Array.IndexOf(acceptableIssuers, issuer) != -1)
                return certificate;
        }
    }
    if (localCertificates != null &&
        localCertificates.Count > 0)
        return localCertificates[0];

    return null;
}

En el ejemplo de código siguiente se muestra cómo crear una instancia de este delegado.

C#
// Server name must match the host name and the name on the host's certificate.
serverName = args[0];
// Create a TCP/IP client socket.
TcpClient client = new TcpClient(serverName,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),
    new LocalCertificateSelectionCallback(SelectLocalCertificate)
    );

Comentarios

Este delegado se usa para construir instancias de la SslStream clase . La SslStream clase se usa para ayudar a proteger la información intercambiada entre un cliente y un servidor. El cliente y el servidor usan este delegado para seleccionar un certificado que se usará para la autenticación.

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