如何在ASP.NET Core 2.0中使用TLS 1.2

max*_*pan 1 c# salesforce asp.net-core-2.0

直到我的salesforce res api都运行良好。突然我开始收到身份验证错误。重试您的请求。 Salesforce.Common.AuthenticationClient.d__1.MoveNext()

salesforce通知它将从现在开始使用TLS .1.2。如何在Startup.cs中强制我的asp.net core 2.0使用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());
            }
        }
Run Code Online (Sandbox Code Playgroud)

Ale*_*uiu 5

4.7版之前的.net框架默认情况下使用TLS 1.0建立出站连接。您可以升级到较新的版本来解决该问题,或者,可以使用ServicePointManager设置出站呼叫的默认版本和后备版本,或者,如果您具有库的源代码,则将该设置传递给HttpClient。

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

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
Run Code Online (Sandbox Code Playgroud)

您可以在此处找到关于此主题的良好描述:https : //social.msdn.microsoft.com/Forums/zh-CN/8db54c83-1329-423b-8d55-4dc6a25fe826/how-to-make-a-web-client -app-use-tls-12?forum = csharpgeneral

如果你想将其指定仅用于某些请求,而不是应用程序范围内,那么你可以自定义HttpClientHandler你的HttpClient

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

HttpClient client = new HttpClient(handler);
...
Run Code Online (Sandbox Code Playgroud)


小智 5

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

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