Configure ServicePointManager.SecurityProtocol through AppSettings
As time passes, webservers move to newer security protocols and remove deprecated security protocols. This may lead to errors in your application when you make HTTP requests. Usually .NET automatically finds a security protocol in common, but sometimes you have to update ServicePointManager.SecurityProtocol explicitly.
You can remove security protocols using a binary AND operator and a Bitwise complement to inverse the bits:
You can add security protocols using a Binary OR operator:
Instead of hardcoding the additions/removals into your codebase, you could also use config-files with appSettings. Here's an example of what your configuration file would look like:
In your application, run the following methods RemoveSecurityProtocols then AddSecurityProtocols:
Here's a sample .NET Framework console application that sets the available security protocols to SSL3 only. This will intentionally fail to match security protocols when requesting 'swimburger.net':
The console output will look like this:
As you can see by the output, an HttpRequestException is thrown and the inner inner exception is "System.ComponentModel.Win32Exception: The client and server cannot communicate, because they do not possess a common algorithm".
When you add TLS1.1 & 1.2 back by running RemoveSecurityProtocols then AddSecurityProtocols, the HTTP request succeeds:
The output looks like this:
Here's the entire console sample: