System.Net.Security

SslStream.EndSecureChannel Method

Terminates an SSL/TLS connection.

public void SslStream.EndSecureChannel( IAsyncResult asyncResult );

Parameters

Remarks

The EndSecureChannel method completes the asynchronous operation started by a call to BeginSecureChannel. You must call this method before you can reuse the SslStream object for another secure channel operation.

If the asynchronous operation has not yet completed, this method blocks until it completes.

Note

If the BeginSecureChannel method was called with a callback delegate, you must call EndSecureChannel with the same IAsyncResult object to ensure proper resource cleanup and to catch any exceptions that occurred during the asynchronous operation.

Example


using System;
using System.Net.Security;
using System.Net.Sockets;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

public class SslExample
{
    public static async Task PerformSslOperation(TcpClient client, X509Certificate certificate)
    {
        using (SslStream sslStream = new SslStream(client.GetStream()))
        {
            try
            {
                // Authenticate as server
                sslStream.BeginAuthenticateAsServer(certificate, (asyncResult) =>
                {
                    try
                    {
                        sslStream.EndAuthenticateAsServer(asyncResult);
                        Console.WriteLine("Server authenticated.");

                        // Perform secure communication...
                        // For demonstration, we'll just end the secure channel.

                        // End the secure channel operation
                        sslStream.BeginEndSecureChannel(null, (endResult) =>
                        {
                            try
                            {
                                sslStream.EndEndSecureChannel(endResult);
                                Console.WriteLine("Secure channel ended successfully.");
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine($"Error ending secure channel: {ex.Message}");
                            }
                        }, null);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine($"Authentication error: {ex.Message}");
                    }
                }, null);

                // In a real application, you'd likely await or manage the authentication
                // task completion more directly. This is a simplified async callback example.
                await Task.Delay(1000); // Simulate some time for authentication to complete
            }
            catch (Exception ex)
            {
                Console.WriteLine($"General SSL error: {ex.Message}");
            }
        }
    }

    // Dummy certificate and client for example completeness
    public static X509Certificate GetDummyCertificate()
    {
        // In a real scenario, load your actual server certificate
        return new X509Certificate2("server.pfx", "password");
    }

    public static TcpClient GetDummyClient()
    {
        // In a real scenario, this would be a connected client
        return new TcpClient();
    }

    public static async Task Main(string[] args)
    {
        // Example usage (requires a dummy certificate and client setup)
        // var certificate = GetDummyCertificate();
        // var client = GetDummyClient();
        // await PerformSslOperation(client, certificate);
        Console.WriteLine("Example usage requires actual certificate and client setup.");
    }
}
            

Requirements

Product Latest Supported
.NET Framework 4.8 4.0
.NET Core 3.1 3.0
.NET 5+ 8.0 5.0

See Also