The SslProtocolDictionary class provides a mapping between SSL protocol names (strings) and their corresponding SslProtocols enum values. This is useful for configuring SSL/TLS connections when the protocol is specified as a string, such as in configuration files or user input.
Syntax
public sealed class SslProtocolDictionary
Remarks
This class is immutable and thread-safe. It provides methods to add or retrieve protocol mappings. The primary use case is to translate human-readable protocol names (e.g., "Tls12", "Ssl3") into the underlying SslProtocols enumeration values that can be used by .NET security classes like SslStream.
Note: It is important to use modern and secure TLS protocols. Avoid enabling older, less secure protocols like SSL 2.0 and SSL 3.0 unless absolutely necessary for compatibility with legacy systems.
Methods
Add(string protocolName, SslProtocols protocolValue)
Adds a new mapping from a protocol name string to an SslProtocols enum value.
Example:
SslProtocolDictionary dict = new SslProtocolDictionary();
dict.Add("Tls13", SslProtocols.Tls13);
dict.Add("Negotiate", SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12);
Contains(string protocolName)
Checks if a mapping for the given protocol name exists in the dictionary.
Example:
SslProtocolDictionary dict = new SslProtocolDictionary();
dict.Add("Tls12", SslProtocols.Tls12);
bool exists = dict.Contains("Tls12"); // exists will be true
Get(string protocolName)
Retrieves the SslProtocols enum value associated with the given protocol name. Throws an exception if the name is not found.
Example:
SslProtocolDictionary dict = new SslProtocolDictionary();
dict.Add("Tls11", SslProtocols.Tls11);
SslProtocols protocol = dict.Get("Tls11"); // protocol will be SslProtocols.Tls11
TryGetValue(string protocolName, out SslProtocols protocolValue)
Attempts to retrieve the SslProtocols enum value associated with the given protocol name without throwing an exception. Returns true if the name is found, false otherwise.
Example:
SslProtocolDictionary dict = new SslProtocolDictionary();
dict.Add("Tls13", SslProtocols.Tls13);
SslProtocols foundProtocol;
bool success = dict.TryGetValue("Tls13", out foundProtocol); // success will be true, foundProtocol will be SslProtocols.Tls13
bool failure = dict.TryGetValue("InvalidProtocol", out foundProtocol); // failure will be false
Default Mappings
The SslProtocolDictionary typically comes with a set of default mappings, which may include common protocol names. These can vary slightly between .NET versions.
| Protocol Name (String) | Corresponding SslProtocols Value |
|---|---|
"Ssl2" |
SslProtocols.Ssl2 |
"Ssl3" |
SslProtocols.Ssl3 |
"Tls" |
SslProtocols.Tls |
"Tls10" |
SslProtocols.Tls10 |
"Tls11" |
SslProtocols.Tls11 |
"Tls12" |
SslProtocols.Tls12 |
"Tls13" |
SslProtocols.Tls13 |
"Default" |
SslProtocols.None (or system default) |
"Any" |
SslProtocols.Tls | SslProtocols.Tls11 | SslProtocols.Tls12 | SslProtocols.Tls13 (or similar combination) |