如何在asp.net core 2.0中使用TLS 1.2

问题描述 投票:0回答:2

我的salesforce res apis工作正常,直到。突然间我开始收到身份验证错误。重试您的请求。 Salesforce.Common.AuthenticationClient.d__1.MoveNext()。

salesforce告知它将从现在开始使用TLS .1.2。如何强制我的asp.net核心2.0在startup.cs中使用TLS 1.2。下面是我的登录代码。

 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());
            }
        }
c# salesforce asp.net-core-2.0
2个回答
1
投票

版本4.7之前的.net框架默认使用TLS 1.0进行出站连接。您可以升级到较新版本以解决问题,或者,您可以使用ServicePointManager为出站呼叫设置默认和回退版本,或者如果您拥有库的源代码,则将设置传递给HttpClient。

在管道的早期添加以下内容,例如startup.csglobal.asax

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

你可以在这里找到关于这个主题的很好的描述: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

如果您只想为某些请求而不是应用程序范围指定它,那么您可以自定义HttpClientHandlerHttpClient

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

HttpClient client = new HttpClient(handler);
...

0
投票

根据以下https://github.com/dotnet/corefx/issues/29452

在.NET Core中,ServicePointManager仅影响HttpWebRequest。它不会影响HttpClient。您应该能够使用HttpClientHandler.ServerCertificateValidationCallback来实现相同的功能。

© www.soinside.com 2019 - 2024. All rights reserved.