Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

LocalCertificateSelectionCallback Delegate

Selects the local Secure Sockets Layer (SSL) certificate used for authentication.

Namespace:  System.Net.Security
Assembly:  System (in System.dll)
public delegate X509Certificate LocalCertificateSelectionCallback(
	Object sender,
	string targetHost,
	X509CertificateCollection localCertificates,
	X509Certificate remoteCertificate,
	string[] acceptableIssuers
)

Parameters

sender
Type: System.Object

An object that contains state information for this validation.

targetHost
Type: System.String

The host server specified by the client.

localCertificates
Type: System.Security.Cryptography.X509Certificates.X509CertificateCollection

An X509CertificateCollection containing local certificates.

remoteCertificate
Type: System.Security.Cryptography.X509Certificates.X509Certificate

The certificate used to authenticate the remote party.

acceptableIssuers
Type: System.String[]

A String array of certificate issuers acceptable to the remote party.

Return Value

Type: System.Security.Cryptography.X509Certificates.X509Certificate
An X509Certificate used for establishing an SSL connection.

This delegate is used to construct instances of the SslStream class. The SslStream class is used to help secure information exchanged between a client and server. The client and server use this delegate to select a certificate to be used for authentication.

The following code example demonstrates a method implementation for this delegate.

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

The following code example demonstrates creating an instance of this delegate.

// 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,80);
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)
    );

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.