Edit

Share via


SslApplicationProtocol Struct

Definition

Represents a value of TLS Application protocol.

C#
public readonly struct SslApplicationProtocol : IEquatable<System.Net.Security.SslApplicationProtocol>
Inheritance
SslApplicationProtocol
Implements

Examples

The following code example demonstrates negotiation of the application-level protocol on SslStream. The server advertises support for protocol1 and protocol2. The client advertises support for protocol2 and protocol3. The commonly supported protocol (protocol2) gets negotiated during the handshake.

C#
async Task Server(NetworkStream stream, X509Certificate2 serverCertificate)
{
    using var server = new SslStream(stream);

    await server.AuthenticateAsServerAsync(new SslServerAuthenticationOptions
    {
        ServerCertificate = serverCertificate,
        ApplicationProtocols = new()
        {
            new("protocol1"),
            new("protocol2"),
        }
    });

    string protocol = Encoding.ASCII.GetString(server.NegotiatedApplicationProtocol.Protocol.Span);
    System.Console.WriteLine($"Server - negotiated protocol: {protocol}");
}

async Task Client(NetworkStream stream, string hostName)
{
    using var client = new SslStream(stream);

    await client.AuthenticateAsClientAsync(new SslClientAuthenticationOptions
    {
        // the host name must match the name on the certificate used on the server side
        TargetHost = hostName,
        ApplicationProtocols = new()
        {
            new("protocol2"),
            new("protocol3")
        }
    });

    string protocol = Encoding.ASCII.GetString(client.NegotiatedApplicationProtocol.Protocol.Span);
    System.Console.WriteLine($"Client - negotiated protocol: {protocol}");
}

// possible output:
//   Server - negotiated protocol: protocol2
//   Client - negotiated protocol: protocol2

Remarks

This type contains static fields with predefined SslApplicationProtocol values for HTTP versions.

During the handshake, the client sends a list of available ALPN protocols and the server chooses the best match from that list.

For a complete list of supported protocols, see TLS Application-Layer Protocol Negotiation (ALPN) Protocol IDs.

Constructors

SslApplicationProtocol(Byte[])

Initializes a new instance of the SslApplicationProtocol.

SslApplicationProtocol(String)

Initializes a new instance of the SslApplicationProtocol.

Fields

Http11

Gets a SslApplicationProtocol representing HTTP/1.1 TLS application protocol.

Http2

Gets a SslApplicationProtocol representing HTTP/2 TLS application protocol.

Http3

Defines a SslApplicationProtocol instance for HTTP 3.0.

Properties

Protocol

Gets a current TLS application protocol represented by this SslApplicationProtocol.

Methods

Equals(Object)

Compares the SslApplicationProtocol to the specified object.

Equals(SslApplicationProtocol)

Compares a SslApplicationProtocol to the specified SslApplicationProtocol instance.

GetHashCode()

Returns the hash code for the SslApplicationProtocol instance.

ToString()

Overrides the ToString() method.

Operators

Applies to

Product Versions
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1