簡體   English   中英

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

[英]How to use TLS 1.2 in 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());
            }
        }

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/zh-CN/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);
...

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

在.NET Core中,ServicePointManager僅影響HttpWebRequest。 它不會影響HttpClient。 您應該能夠使用HttpClientHandler.ServerCertificateValidationCallback來實現相同的目的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM