Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 请求已中止:无法创建SSL/TLS安全通道异常_C#_Exception_Iis_Service_Web - Fatal编程技术网

C# 请求已中止:无法创建SSL/TLS安全通道异常

C# 请求已中止:无法创建SSL/TLS安全通道异常,c#,exception,iis,service,web,C#,Exception,Iis,Service,Web,让我解释一下我的情况 我创建了一个自签名证书,并将其安装在MMC中的受信任的根证书颁发机构部分 然后,我使用自签名证书创建了两个证书: 使用者名称为“localhost”的证书 主题名为“test.com”的证书 然后,我将这两个证书安装到MMC中的个人证书部分 然后,我在IIS中将web服务部署为HTTPS(带有接受客户端证书的SSL)。用于部署web服务的证书是使用者名称为“localhost”的证书 现在,我有一个客户端想要连接到web服务。我成功地向该服务添加了一个web引用。代码如下:

让我解释一下我的情况

我创建了一个自签名证书,并将其安装在MMC中的受信任的根证书颁发机构部分

然后,我使用自签名证书创建了两个证书:

  • 使用者名称为“localhost”的证书
  • 主题名为“test.com”的证书
  • 然后,我将这两个证书安装到MMC中的个人证书部分

    然后,我在IIS中将web服务部署为HTTPS(带有接受客户端证书的SSL)。用于部署web服务的证书是使用者名称为“localhost”的证书

    现在,我有一个客户端想要连接到web服务。我成功地向该服务添加了一个web引用。代码如下:

                ClientServices web_service = new ClientServices();
                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection col = store.Certificates.Find(X509FindType.FindBySubjectName, "test.com", true);
    
                if (col.Count == 1)
                {
                    ServicePointManager.Expect100Continue = true;
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls;
                    web_service.ClientCertificates.Add(col[0]);
    
                    try
                    {
                        string hello = web_service.HelloWorld();
                        int add = web_service.add(4, 31);
                        int sub = web_service.subtract(30, 10);
    
                        Console.WriteLine(hello);
                        Console.WriteLine(add);
                        Console.WriteLine(sub);
                    }
                    catch (WebException e)
                    {
                        Console.WriteLine(e.Message.ToString());
                    }
                }
    
                else
                {
                    Console.WriteLine("The certificate was not found!");
                }
                Console.ReadKey();
    
    如您所见,我正在发送“test.com”证书以及web服务请求。不幸的是,我遇到了以下例外情况:

    The request was aborted: Could not create SSL/TLS secure channel
    
    我怎样才能解决这个问题?我已经在这个问题上浪费了3个小时。请帮帮我

    private void Somewhere() {
        ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(AllwaysGoodCertificate);
    }
    
    private static bool AllwaysGoodCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors policyErrors) {
       return true;
    }
    
    资料来源:

    谢谢你的提示。通过从头开始重新生成证书,我成功地解决了这个问题。然后它就完美地工作了。我不知道为什么,但后来成功了。