首页 > 解决方案 > System.Net.WebException:请求被中止:无法创建 SSL/TLS 安全通道 - dot net framework 4.7.1

问题描述

我收到以下错误,我已验证证书已正确安装,指纹很好。下面是我的代码。

On Running below command from Powershell :
[Net.ServicePointManager]::SecurityProtocol it shows as SSL3, TLS

我正在使用点网框架 4.7.1。

System.Net.WebException:请求被中止:无法创建 SSL/TLS 安全通道。

web.config :

<httpRuntime requestValidationType="SARQA.NET.BaseCode.NoValidationRequestValidator" 
 maxRequestLength="4096" executionTimeout="180" targetFramework="4.7.1" />

    public static WebRequestHandler getHandlerWithClientCert(string thumbprint)
            {
    ServicePointManager.Expect100Continue = true;
   ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | 
    SecurityProtocolType.Tls12;

                var wrhandler = new WebRequestHandler
                {
                    AutomaticDecompression = System.Net.DecompressionMethods.GZip |
                                             System.Net.DecompressionMethods.Deflate
                };

                wrhandler.ClientCertificates.Add(GetCertFromStore(thumbprint));

                return wrhandler;
            }


public static X509Certificate2 GetCertFromStore(string thumbprint)
        {            
            X509Certificate2 cert = null;

            //removing unwanted char from hexadecimal thumbprint
            thumbprint = Regex.Replace(thumbprint, @"[^\da-fA-F]", string.Empty).ToUpper();

#if(DEBUG)
            var store = new X509Store(StoreName.Root,StoreLocation.CurrentUser);
#else
            var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
#endif
            store.Open(OpenFlags.ReadOnly);

            X509Certificate2Collection certCollection = store.Certificates.Find(
                    X509FindType.FindByThumbprint, thumbprint, true
            );

            if (certCollection.Count > 0)
            {
                cert = certCollection[0];

            }


            store.Close();

            return cert;
        }

标签: c#certificatetls1.2

解决方案


推荐阅读