LocalCertificateSelectionCallback Делегат

Определение

Выбирает локальный сертификат SSL, используемый для проверки подлинности.

public delegate System::Security::Cryptography::X509Certificates::X509Certificate ^ LocalCertificateSelectionCallback(System::Object ^ sender, System::String ^ targetHost, X509CertificateCollection ^ localCertificates, X509Certificate ^ remoteCertificate, cli::array <System::String ^> ^ acceptableIssuers);
public delegate System.Security.Cryptography.X509Certificates.X509Certificate LocalCertificateSelectionCallback(object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate? remoteCertificate, string[] acceptableIssuers);
public delegate System.Security.Cryptography.X509Certificates.X509Certificate LocalCertificateSelectionCallback(object sender, string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate, string[] acceptableIssuers);
type LocalCertificateSelectionCallback = delegate of obj * string * X509CertificateCollection * X509Certificate * string[] -> X509Certificate
Public Delegate Function LocalCertificateSelectionCallback(sender As Object, targetHost As String, localCertificates As X509CertificateCollection, remoteCertificate As X509Certificate, acceptableIssuers As String()) As X509Certificate 

Параметры

sender
Object

Объект, содержащий сведения о состоянии для данной проверки.

targetHost
String

Сервер, указанный клиентом.

localCertificates
X509CertificateCollection

Коллекция X509CertificateCollection, содержащая локальные сертификаты.

remoteCertificate
X509Certificate

Сертификат, используемый для проверки подлинности удаленной стороны.

acceptableIssuers
String[]

Массив элементов String, определяющий поставщиков сертификатов, приемлемых для удаленной стороны.

Возвращаемое значение

Сертификат X509Certificate, используемый для установки SSL-соединения.

Примеры

В следующем примере кода демонстрируется реализация метода для этого делегата.

static X509Certificate^ SelectLocalCertificate(
        Object^ sender, 
        String^ targetHost, 
        X509CertificateCollection^ localCertificates, 
        X509Certificate^ remoteCertificate, 
        array<String^>^ acceptableIssuers
)
{	
    Console::WriteLine("Client is selecting a local certificate.");
    if (acceptableIssuers != nullptr && 
            acceptableIssuers->Length > 0 &&
            localCertificates != nullptr &&
            localCertificates->Count > 0)
    {
        // Use the first certificate that is from an acceptable issuer.
        IEnumerator^ myEnum1 = localCertificates->GetEnumerator();
        while ( myEnum1->MoveNext() )
        {
            X509Certificate^ certificate = safe_cast<X509Certificate^>(myEnum1->Current);
            String^ issuer = certificate->Issuer;
            if ( Array::IndexOf( acceptableIssuers, issuer ) != -1 )
                return certificate;
        }
    }
    if (localCertificates != nullptr &&
            localCertificates->Count > 0)
        return localCertificates[0];
            
    return nullptr;
 }
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;
}

В следующем примере кода показано создание экземпляра этого делегата.

// Server name must match the host name and the name on the host's certificate. 
serverName = args[ 1 ];

// Create a TCP/IP client socket.
TcpClient^ client = gcnew TcpClient( serverName,5000 );
Console::WriteLine( L"Client connected." );

// Create an SSL stream that will close the client's stream.
SslStream^ sslStream = gcnew SslStream( 
    client->GetStream(),
    false,
    gcnew RemoteCertificateValidationCallback( ValidateServerCertificate ),
    gcnew LocalCertificateSelectionCallback( SelectLocalCertificate ) );
// 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)
    );

Комментарии

Этот делегат используется для создания экземпляров SslStream класса . Класс SslStream используется для защиты информации, обмениваемой между клиентом и сервером. Клиент и сервер используют этот делегат для выбора сертификата, который будет использоваться для проверки подлинности.

Методы расширения

GetMethodInfo(Delegate)

Получает объект, представляющий метод, представленный указанным делегатом.

Применяется к

См. также раздел