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应用程序级协议的协商。 服务器播发对 protocol1protocol2的支持。 客户端播发对 protocol2protocol3的支持。 通常支持的协议 (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) 协议 ID

构造函数

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 对象的不相等运算符。

适用于