Redigeeri

Jagamisviis:


SslApplicationProtocol Struct

Definition

Represents a value of TLS Application protocol.

public value class SslApplicationProtocol : IEquatable<System::Net::Security::SslApplicationProtocol>
public readonly struct SslApplicationProtocol : IEquatable<System.Net.Security.SslApplicationProtocol>
type SslApplicationProtocol = struct
Public Structure SslApplicationProtocol
Implements IEquatable(Of 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.

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
Async Function Server(stream As NetworkStream, serverCertificate As X509Certificate2) As Task
    Using serverStream As SslStream = new SslStream(stream)
        Dim options as New SslServerAuthenticationOptions() With
        {
            .ServerCertificate = serverCertificate,
            .ApplicationProtocols = New List(Of SslApplicationProtocol) From
            {
                New SslApplicationProtocol("protocol1"),
                New SslApplicationProtocol("protocol2")
            }
        }
        Await serverStream.AuthenticateAsServerAsync(options)

        Dim protocol As String = Encoding.ASCII.GetString(
            serverStream.NegotiatedApplicationProtocol.Protocol.Span)
        System.Console.WriteLine($"Server - negotiated protocol: {protocol}")
    End Using
End Function

Async Function Client(stream As NetworkStream, hostName As String ) As Task
    Using clientStream As SslStream = new SslStream(stream)
        Dim options as New SslClientAuthenticationOptions() With
        {
            .TargetHost = hostName,
            .ApplicationProtocols = New List(Of SslApplicationProtocol) From
            {
                New SslApplicationProtocol("protocol2"),
                New SslApplicationProtocol("protocol3")
            }
        }
        Await clientStream.AuthenticateAsClientAsync(options)

        Dim protocol As String = Encoding.ASCII.GetString(
            clientStream.NegotiatedApplicationProtocol.Protocol.Span)
        System.Console.WriteLine($"Client - negotiated protocol: {protocol}")
    End Using
End Function

' 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

Equality(SslApplicationProtocol, SslApplicationProtocol)

The equality operator for comparing two SslApplicationProtocol objects.

Inequality(SslApplicationProtocol, SslApplicationProtocol)

The inequality operator for comparing two SslApplicationProtocol objects.

Applies to