C# – TLS 1. 2 The client and server cannot communicate, because they do not possess a common algorithm

cnettls1.2windows

I have an issue with TLS 1.2.
It works if I enable tls1.0 in client machine, but it is not recommended.

Exception is – The client and server cannot communicate, because they
do
not possess a common algorithmSystem.ComponentModel.Win32Exception
(0x80004005): The client and server cannot communicate, because they do
not possess a common algorithm.

Target Framework of my app is .NET 4.6.2.

Best Answer

There are two possible scenario, in my case I used 2nd point.

  1. If you are facing this issue in production environment and you can easily deploy new code to the production then you can use of below solution.

    You can add below line of code before making api call,

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; // .NET 4.5

  2. If you cannot deploy new code and you want to resolve with the same code which is present in the production, then this issue can be done by changing some configuration setting in config file. You can add either of one in your config file.

<runtime>
    <AppContextSwitchOverrides value="Switch.System.Net.DontEnableSchUseStrongCrypto=false"/>
  </runtime>

or

<runtime>
  <AppContextSwitchOverrides value="Switch.System.Net.DontEnableSystemDefaultTlsVersions=false"
</runtime>