3

My salesforce res apis were working fine until. When suddenly I started getting authentication errors. retry your request. Salesforce.Common.AuthenticationClient.d__1.MoveNext().

salesforce informed that it would use from now TLS .1.2. How can I enforce my asp.net core 2.0 to use TLS 1.2 in Startup.cs. below is my code for login.

 private async Task<AuthenticationClient> GetValidateAuthentication()
        {
            RestApiSetting data = new RestApiSetting(Configuration);
            var auth = new AuthenticationClient();
            var url = data.IsSandBoxUser.Equals("true", StringComparison.CurrentCultureIgnoreCase)
                ? "https://test.salesforce.com/services/oauth2/token"
                : "https://login.salesforce.com/services/oauth2/token";
            try
            {
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                await auth.UsernamePasswordAsync(data.ConsumerKey,
                data.ConsumerSecret, data.Username, data.Password, url);
                return auth;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
        }
Andrei Rînea
  • 20,288
  • 17
  • 117
  • 166
maxspan
  • 13,326
  • 15
  • 75
  • 104

2 Answers2

20

.net framework prior to version 4.7 makes outbound connections using TLS 1.0 by default. You can upgrade to a newer version to fix the problem, or alternatively, you can set the default and fallback versions for outbound calls using the ServicePointManager, or passing the setting into the HttpClient if you have the source code for the library.

Add the following somewhere early in your pipeline, such as your startup.cs or global.asax:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

You can find a good description on the topic here: https://social.msdn.microsoft.com/Forums/en-US/8db54c83-1329-423b-8d55-4dc6a25fe826/how-to-make-a-web-client-app-use-tls-12?forum=csharpgeneral

And if you want to specify it only for some requests instead of application-wide, then you can customize the HttpClientHandler of your HttpClient:

var handler = new HttpClientHandler
{
    SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls
};

HttpClient client = new HttpClient(handler);
...
Alexandru Puiu
  • 741
  • 6
  • 16
  • A note to others coming with 4.6.1 targeting issues where you may be dragging in deps to .netstandard as a result of other nuget packages. `ServicePointManager.SecurityProtocol` does not seem to work in some cases (due to binding conflict/resolution issue). Setting `HttpClientHandler.SslProtocols` explicitly and passing to `HttpClient` resolved this for me. – Andrew Nov 27 '20 at 10:06
5

According to the following https://github.com/dotnet/corefx/issues/29452

In .NET Core, ServicePointManager affects only HttpWebRequest. It does not affect HttpClient. You should be able to use HttpClientHandler.ServerCertificateValidationCallback to achieve the same.

G.Q. Luo
  • 71
  • 1
  • 2