亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

“請求被中止:無法創建 SSL/TLS 安全通道”第一次訪問 API 時

“請求被中止:無法創建 SSL/TLS 安全通道”第一次訪問 API 時

C#
森欄 2022-12-24 09:33:51
我正在嘗試從 Web API 使用客戶端的 Web 服務,下面是我們目前用來繞過 SSL 證書的代碼ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;它工作正常,直到他們最終禁用了 TLS 1.0 和 TLS 1.1?,F在我們添加了以下代碼以使用 TLS 1.2 進行客戶端服務器連接ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; ServicePointManager.ServerCertificateValidationCallback += (sender, cert, chain, sslPolicyErrors) => true;現在我收到“請求已中止:無法創建 SSL/TLS 安全通道?!?nbsp;僅當我第一次訪問 API 時出錯,然后如果我連續訪問 API 會得到結果,如果我等待大約一分鐘左右的時間,再次僅在第一次訪問時出現相同的錯誤。
查看完整描述

3 回答

?
精慕HU

TA貢獻1845條經驗 獲得超8個贊

設置安全協議類型需要在創建發布請求之前完成。所以這:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

應該出現在這之前:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

因此,如果您看到它對后續請求有效,則可能是您設置協議的時間太晚了。


查看完整回答
反對 回復 2022-12-24
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

以下代碼可用于幫助解決問題。


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

ServicePointManager.ServerCertificateValidationCallback += ValidateServerCertificate;


...


private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)

{

    // If the certificate is a valid, signed certificate, return true to short circuit any add'l processing.

    if (sslPolicyErrors == SslPolicyErrors.None)

    {

        return true;

    }

    else

    {

        // cast cert as v2 in order to expose thumbprint prop - if needed

        var requestCertificate = (X509Certificate2)certificate;


        // init string builder for creating a long log entry

        var logEntry = new StringBuilder();


        // capture initial info for the log entry

        logEntry.AppendFormat("SSL Policy Error(s): {0} - Cert Issuer: {1} - SubjectName: {2}",

           sslPolicyErrors.ToString(),

           requestCertificate.Issuer,

           requestCertificate.SubjectName.Name);


        // check for other error types as needed

        if (sslPolicyErrors == SslPolicyErrors.RemoteCertificateChainErrors) //Root CA problem

        {

            // check chain status and log

            if (chain != null && chain.ChainStatus != null)

            {

                // check errors in chain and add to log entry

                foreach (var chainStatus in chain.ChainStatus)

                {

                    logEntry.AppendFormat("|Chain Status: {0} - {1}", chainStatus.Status.ToString(), chainStatus.StatusInformation.Trim());

                }

            }

        }


        // replace with your logger

        MyLogger.Info(logEntry.ToString().Trim());

    }


    return false;

}


查看完整回答
反對 回復 2022-12-24
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

對于運行 .NET 版本 4 的用戶,他們可以使用下面的


ServicePointManager.SecurityProtocol = CType(768, SecurityProtocolType) Or CType(3072,SecurityProtocolType)

ServicePointManager.Expect100Continue = True


查看完整回答
反對 回復 2022-12-24
  • 3 回答
  • 0 關注
  • 393 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號