【问题标题】:.Net Core Connected Service with SSL Certificate带有 SSL 证书的 .Net Core 连接服务
【发布时间】:2019-12-11 08:33:52
【问题描述】:

我正在尝试向使用 SSL 证书进行通信的 Web 服务端点发出请求。我花了几个小时在谷歌上搜索一个例子,但到目前为止几乎没有。

我确实设法通过直接导航到 wsdl 和 xsd 文件、手动保存它们并将 WCF Web 服务引用提供程序指向基于this 解决方案的包含目录来使连接的服务成为脚手架。我还尝试使用 winhttpcertcfg.exe 安装证书,但无法使用该工具成功打开通道以直接从 WSDL 生成客户端。

现在我已经生成了客户端,但我无法弄清楚如何正确添加证书。这是我目前拥有的代码

 // Get the certificate
                var testCert = new X509Certificate2(System.IO.File.ReadAllBytes("C://SecureCert.PFX"), "##########");

                //Create instance of SOAP client
                HostedCollectionPaymentService.OnlineService_v2_2Client soapClient = new OnlineService_v2_2Client(new BasicHttpsBinding(BasicHttpsSecurityMode.Transport), new EndpointAddress("https://secure.service.endpoint.com/2.2/"));

                // Add the certificate to the client
                soapClient.ClientCredentials.ClientCertificate.Certificate = testCert;

                using (new OperationContextScope(soapClient.InnerChannel))
                {
                    try
                    {
                        var result = await soapClient.startOnlineCollectionAsync(new StartOnlineCollectionRequest
                        {
                            app_id = "12344",
                            tracking_id = "fdsa43531",
                            transaction_amount = 5.00m,
                            transaction_type = TransactionType.Sale
                        });

                        Console.WriteLine(result.startOnlineCollectionResponse.token);
                    }
                    catch (Exception ex)
                    {
                        var f = ex;
                        throw;
                    }
                }

当我尝试连接时,我收到响应“消息 =”无法为 SSL/TLS 安全通道建立信任关系,授权为 'secure.service.endpoint.com'”。

我已验证证书有效,并且我能够使用 SoapUI 工具集连接到服务。

我假设我缺少配置或错误地附加了 SSL 证书。如果有人可以提供建议或向我指出适当的文档,我将不胜感激。

【问题讨论】:

    标签: c# ssl asp.net-core soap certificate


    【解决方案1】:

    想通了。我需要这个额外的配置行。

    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
    

    这是一个示例,供那些询问它包含在何处的人使用。就我而言,它用于支付网关服务。

    // Get the cert
    var myCertificate = await GetMyCertificate(); //X509Cert
    
    // Create a new binding to specify certificate security
    var binding = new BasicHttpsBinding()
    {
        Name = "basic_ssl_cert"
    };
    
    // Specify the credential type
    binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
    
    
    //Create instance of SOAP client
    QaPaymentService.PaymentOnlineService_v2_Client soapClient = new QaPaymentService.PaymentOnlineService_v2_Client(binding, new EndpointAddress(onlinePaymentServiceEndpoint));
    
    // Add the certificate to the client
    soapClient.ClientCredentials.ClientCertificate.Certificate = myCertificate;
    
    
    using (new OperationContextScope(soapClient.InnerChannel))
    {
        try
        {
            var result = soapClient.completeOnlineCollectionAsync(new QaPaymentService.CompleteOnlineCollectionRequest
            {
                app_id = appId,
                token = token           
            }).GetAwaiter().GetResult();
    
            return (result.completeOnlineCollectionResponse.tracking_id);
        }
        catch (FaultException<QaPaymentService.PaymentServiceFault> ex)
        {
            // Extract the actuall error from the service fault
            throw new myServiceException(ex.Detail.return_detail, ex)
            {
                ErrorDetail = ex.Detail.return_detail,
                ErrorCode = ex.Detail.return_code
            };                       
        }
        catch (Exception ex)
        {
            logger.LogError($"Error completing transaction from QA my service: {ex.Message}", ex);
            throw ex;
        }
    }           
    

    【讨论】:

    • 你在哪里添加了这一行?
    • 感谢分享!我也错过了那条线。我花了几天时间试图找出问题所在!
    猜你喜欢
    • 1970-01-01
    • 2011-12-31
    • 2013-07-13
    • 2016-10-15
    • 2012-01-14
    • 1970-01-01
    • 2016-07-12
    • 1970-01-01
    • 2019-04-26
    相关资源
    最近更新 更多