.net - net WCF 客户端配置 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://docs.microsoft.com/en-us/dotnet/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'。
我的问题是如何在客户端配置 de 端点
解决方案
我找到了解决方案。,对我有用。
设置安全协议
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
设置证书
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));
推荐阅读
- sql - 如何使用 SQL Server 将“01020304”更改为“01,02,03,04”?
- google-cloud-platform - 谷歌云函数部署失败
- python - seaborn.pairplot() 改变每个图的颜色
- groovy - SoapUI Groovy 脚本未在 Testcase 中运行
- javascript - 如何使用 vuejs 修复滚动的 div?
- sql-server - SQL Service Broker 模拟中毒消息
- javascript - 如何防止AngularJS中的函数同时执行?
- google-cloud-platform - 如何使用 bq load 命令在 BigQuery 中加载 csv 文件数据?
- flutter - 如何使用 Mockito 在 Flutter 中正确模拟 NavigatorObserver?
- r - 如何在ggplot中安排geom点的顺序?