【问题标题】:How to use TLS 1.2 in ASP.NET Core 2.0如何在 ASP.NET Core 2.0 中使用 TLS 1.2
【发布时间】:2018-08-30 03:39:13
【问题描述】:

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

salesforce 告知它将从现在开始使用 TLS .1.2。如何强制我的 asp.net core 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


【解决方案1】:

.net 4.7 之前的框架默认使用 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);
...

【讨论】:

  • 对其他 4.6.1 目标问题的说明,由于其他 nuget 包,您可能会将 deps 拖到 .netstandard 中。 ServicePointManager.SecurityProtocol 在某些情况下似乎不起作用(由于绑定冲突/解决问题)。明确设置HttpClientHandler.SslProtocols 并传递给HttpClient 为我解决了这个问题。
【解决方案2】:

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

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

【讨论】:

  • 那么使用 Alexandru Puiu 为特定 HttpClient 自定义处理程序的方法应该可以正常工作吗?
  • ServerCertificateValidationCallback 验证证书,SslProtocols 设置要使用的 TLS 版本
猜你喜欢
  • 2019-06-06
  • 2020-12-06
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多