C# 如何在ASP.NET Core 2.0中使用TLS 1.2
我的salesforce res API在之前一直工作良好。当我突然发现身份验证错误时重试您的请求。 Salesforce.Common.AuthenticationClient.d\u 1.MoveNext() salesforce告知从现在起将使用TLS.1.2。如何强制我的asp.net core 2.0在Startup.cs中使用TLS 1.2。下面是我的登录代码C# 如何在ASP.NET Core 2.0中使用TLS 1.2,c#,salesforce,asp.net-core-2.0,C#,Salesforce,Asp.net Core 2.0,我的salesforce res API在之前一直工作良好。当我突然发现身份验证错误时重试您的请求。 Salesforce.Common.AuthenticationClient.d\u 1.MoveNext() salesforce告知从现在起将使用TLS.1.2。如何强制我的asp.net core 2.0在Startup.cs中使用TLS 1.2。下面是我的登录代码 private async Task<AuthenticationClient> GetValidateAut
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());
}
}
private异步任务GetValidateAuthentication()
{
重启设置数据=新重启设置(配置);
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";
尝试
{
//ServicePointManager.SecurityProtocol=SecurityProtocolType.Tls12;
等待auth.UsernamePasswordAsync(data.ConsumerKey,
data.ConsumerSecret、data.Username、data.Password、url);
返回auth;
}
捕获(例外情况除外)
{
抛出新异常(例如ToString());
}
}
.net framework 4.7版之前的版本默认使用TLS 1.0进行出站连接。您可以升级到较新的版本以解决问题,或者,您可以使用ServicePointManager为出站调用设置默认版本和回退版本,或者如果您有库的源代码,则将设置传递到HttpClient
在管道的早期某处添加以下内容,例如startup.cs
或global.asax
:
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
您可以在此处找到有关该主题的详细说明:
如果只想为某些请求而不是整个应用程序指定,则可以自定义HttpClientHandler
的HttpClientHandler
:
var handler = new HttpClientHandler
{
SslProtocols = SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls
};
HttpClient client = new HttpClient(handler);
...
根据以下
在.NET Core中,ServicePointManager只影响HttpWebRequest。它不影响HttpClient。您应该能够使用HttpClientHandler.ServerCertificateValidationCallback实现同样的功能
也许这会有所帮助,但这并没有告诉我如何以及在何处实现UseHttps。对于.net,我用来运行注册表项的.net core,应该有类似的方法。请检查下面的页面中的.net,我找不到任何AuthenticationClient是salesforce库中的某个类?然后使用Alexandru Puiu的方法为特定的HttpClient应该可以正常工作?ServerCertificateValidationCallback验证证书,SslProtocols设置要使用的TLS版本。请注意,4.6.1针对的问题可能是由于其他nuget软件包而将deps拖到.netstandard<代码>ServicePointManager.SecurityProtocol在某些情况下似乎不起作用(由于绑定冲突/解决问题)。显式设置
HttpClientHandler.SslProtocols
并传递给HttpClient
为我解决了这个问题。