SslApplicationProtocol 结构

定义

表示 TLS 应用程序协议的值。

C#
public readonly struct SslApplicationProtocol : IEquatable<System.Net.Security.SslApplicationProtocol>
继承
SslApplicationProtocol
实现

示例

下面的代码示例演示对 上的 SslStream应用程序级协议的协商。 服务器播发对 protocol1protocol2的支持。 客户端播发对 protocol2protocol3的支持。 通常支持的协议 (protocol2) 在握手期间协商。

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

注解

此类型包含具有 HTTP 版本的预定义 SslApplicationProtocol 值的静态字段。

在握手期间,客户端发送可用 ALPN 协议的列表,服务器将从该列表中选择最佳匹配项。

有关受支持协议的完整列表,请参阅 TLS Application-Layer 协议协商 (ALPN) 协议 ID

构造函数

字段

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() 方法。

运算符

适用于

产品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1