LocalCertificateSelectionCallback 代理人

定義

選取用於驗證的本機 Secure Sockets Layer (SSL) 憑證。

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);

參數

sender
Object

物件,包含這個驗證的狀態資訊。

targetHost
String

由用戶端指定的主機伺服器。

localCertificates
X509CertificateCollection

X509CertificateCollection,包含本機憑證。

remoteCertificate
X509Certificate

憑證,用於驗證遠端群體。

acceptableIssuers
String[]

遠端群體可接受之憑證簽發者的 String 陣列。

傳回值

X509Certificate,用於建立 SSL 連線。

範例

下列程式代碼範例示範這個委派的方法實作。

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;
}

下列程式代碼範例示範如何建立此委派的實例。

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)
    );

備註

這個委派是用來建構 類別的 SslStream 實例。 類別 SslStream 可用來協助保護客戶端與伺服器之間交換的資訊。 用戶端和伺服器會使用此委派來選取要用於驗證的憑證。

擴充方法

GetMethodInfo(Delegate)

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

適用於

產品 版本
.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

另請參閱