Auf Englisch lesen

Freigeben über


SslApplicationProtocol Struktur

Definition

Stellt einen Wert des TLS-Anwendungsprotokolls dar.

C#
public readonly struct SslApplicationProtocol : IEquatable<System.Net.Security.SslApplicationProtocol>
Vererbung
SslApplicationProtocol
Implementiert

Beispiele

Im folgenden Codebeispiel wird die Aushandlung des Protokolls auf Anwendungsebene für SslStreamveranschaulicht. Der Server kündigt Unterstützung für protocol1 und an protocol2. Der Client kündigt Unterstützung für protocol2 und an protocol3. Das häufig unterstützte Protokoll (protocol2) wird während des Handshakes ausgehandelt.

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

Hinweise

Dieser Typ enthält statische Felder mit vordefinierten SslApplicationProtocol Werten für HTTP-Versionen.

Während des Handshakes sendet der Client eine Liste der verfügbaren ALPN-Protokolle, und der Server wählt die beste Übereinstimmung aus dieser Liste aus.

Eine vollständige Liste der unterstützten Protokolle finden Sie unter ALPN-Protokoll-IDs (TLS Application-Layer Protocol Negotiation).

Konstruktoren

SslApplicationProtocol(Byte[])

Initialisiert eine neue Instanz des SslApplicationProtocol.

SslApplicationProtocol(String)

Initialisiert eine neue Instanz des SslApplicationProtocol.

Felder

Http11

Ruft einen SslApplicationProtocol-Wert ab, der für das HTTP-/1.1-TLS-Anwendungsprotokoll steht.

Http2

Ruft einen SslApplicationProtocol-Wert ab, der für das HTTP-/2-TLS-Anwendungsprotokoll steht.

Http3

Definiert eine SslApplicationProtocol instance für HTTP 3.0.

Eigenschaften

Protocol

Ruft ein aktuelles TLS-Anwendungsprotokoll ab, das von SslApplicationProtocol dargestellt wird.

Methoden

Equals(Object)

Vergleicht die SslApplicationProtocol mit dem angegebenen Objekt.

Equals(SslApplicationProtocol)

Vergleicht ein SslApplicationProtocol mit der angegebenen SslApplicationProtocol-Instanz.

GetHashCode()

Gibt den Hashcode für die SslApplicationProtocol-Instanz zurück.

ToString()

Setzt die ToString()-Methode außer Kraft.

Operatoren

Equality(SslApplicationProtocol, SslApplicationProtocol)

Der Gleichheitsoperator zum Vergleichen zweier SslApplicationProtocol-Objekte.

Inequality(SslApplicationProtocol, SslApplicationProtocol)

Der Ungleichheitsoperator (=) zum Vergleichen von zwei SslApplicationProtocol-Objekten.

Gilt für:

Produkt Versionen
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Standard 2.1