共用方式為


SslApplicationProtocol 結構

定義

表示 TLS 應用程式通訊協定的值。

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)
繼承
SslApplicationProtocol
實作

範例

下列程式代碼範例示範 上 SslStream應用層級通訊協定的交涉。 伺服器會公告和protocol2的支援protocol1。 用戶端會公告和protocol3的支援protocol2。 交握期間,通常會交涉支持的通訊協定 protocol2 () 。

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

備註

此類型包含具有 HTTP 版本預先 SslApplicationProtocol 定義值的靜態欄位。

在交握期間,用戶端會傳送可用 ALPN 通訊協定的清單,而伺服器會從該清單中選擇最佳相符專案。

如需支援通訊協定的完整清單,請參閱 TLS Application-Layer 通訊協定交涉 (ALPN) 通訊協定識別碼

建構函式

SslApplicationProtocol(Byte[])

初始化 SslApplicationProtocol 的新執行個體。

SslApplicationProtocol(String)

初始化 SslApplicationProtocol 的新執行個體。

欄位

Http11

取得代表 HTTP/1.1 TLS 應用程式通訊協定的 SslApplicationProtocol

Http2

取得代表 HTTP/2 TLS 應用程式通訊協定的 SslApplicationProtocol

Http3

定義 SslApplicationProtocol HTTP 3.0 的實例。

屬性

Protocol

取得此 SslApplicationProtocol 所代表的目前 TLS 應用程式通訊協定。

方法

Equals(Object)

SslApplicationProtocol 與指定的物件相比較。

Equals(SslApplicationProtocol)

比較 SslApplicationProtocol 和指定的 SslApplicationProtocol 執行個體。

GetHashCode()

傳回 SslApplicationProtocol 執行個體的雜湊程式碼。

ToString()

覆寫 ToString() 方法。

運算子

Equality(SslApplicationProtocol, SslApplicationProtocol)

用來比較兩個 SslApplicationProtocol 物件的等號比較運算子。

Inequality(SslApplicationProtocol, SslApplicationProtocol)

用來比較兩個 SslApplicationProtocol 物件的不等比較運算子。

適用於