簡體   English   中英

net WCF 客戶端配置 https

[英]net WCF client configure htts

我正在嘗試以編程方式連接到 web 服務“https://trackingqa.estafeta.com/Service.asmx”,我的意思是代碼沒有配置 (.config) 我試試這個

BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
        EndpointAddress endpoint = new EndpointAddress(url);
        ServiceSoapClient client = new ServiceSoapClient(binding, endpoint);

但我收到此錯誤:

未提供客戶端證書。 在 ClientCredentials 中指定客戶端證書。

我在 MSDN 上找到了這個文檔( https://learn.microsoft.com/en-us/do.net/framework/wcf/feature-details/transport-security-with-certificate-authentication#configure-the-client

我試試這個:

client.ClientCredentials.ClientCertificate.SetCertificate(
            System.Security.Cryptography.X509Certificates.StoreLocation.CurrentUser,
            System.Security.Cryptography.X509Certificates.StoreName.My,
            System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, "TrackingQA.estafeta.com");

但我收到此錯誤:

異常:找不到具有以下搜索條件的 X.509 證書:StoreName“My”、StoreLocation“CurrentUser”、FindType“FindBySubjectName”、FindValue“TrackingQA.estafeta.com”。

我的問題是如何在客戶端配置端點

我找到了解決方案。對我有用。

  1. 設置安全協議

     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
  2. 設置證書

     client.ClientCredentials.ClientCertificate.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(Convert.FromBase64String(StringCertificateBase64));

完整代碼:

var binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;
        EndpointAddress endpoint = new EndpointAddress(url);
        ServiceSoapClient client= new ServiceSoapClient(binding, endpoint);
        client.ClientCredentials.ClientCertificate.Certificate = new System.Security.Cryptography.X509Certificates.X509Certificate2(Convert.FromBase64String(StringCertificateBase64));

暫無
暫無

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

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