共用方式為


LocalCertificateSelectionCallback 代理人

定義

選取用於驗證的本機 Secure Sockets Layer (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)

取得表示特定委派所代表之方法的物件。

適用於

另請參閱