如何使用WCF服务临时停止证书错误
我正在测试我创建的WCF Web服务的早期版本。 在客户端,当我使用VS来“添加服务引用”时,一切正常。
但是当我尝试使用该服务时,我得到错误,
Could not establish trust relationship for the SSL/TLS secure channel with authority **
星号代表服务器的IP地址。
无论如何在服务器上有一个安全证书,但它只是为了测试自己生成的,所以我暂时不关心证书错误。
在客户端,已经为我生成了app.config,
那么我需要更改哪些设置才能暂时忽略证书错误?
将CertificatePolicy PRIOR设置为在客户端上初始化WCF服务。 这是如何(只需调用一次SetCertificatePolicy()方法)
/// /// Sets the cert policy. /// private static void SetCertificatePolicy() { ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate; } /// /// Certificate validation callback /// private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error) { if (error == SslPolicyErrors.None) { return true; // already determined to be valid } switch (cert.GetCertHashString()) { // thumbprints/hashes of allowed certificates (uppercase) case "066CF9CAD814DE2097D368F22D3A7D398B87C4D6": case "5B82C96685E3A20079B8CE7AFA32554D55DB9611": Debug.WriteLine("Trusting X509Certificate '" + cert.Subject + "'"); return true; default: return false; } }
这对我有用。 谢谢
检查这个问题的答案:
如何告诉WCF跳过证书validation?
它提供了两种可能的解决方案:1。仅在客户端使用配置条目或2.使用使用代码和配置条目的自定义证书validation器
修改web.config对我有用
我是用Steve Ellinger的回答和一些谷歌搜索来做到的。 基本上,我不得不:
这是web.config片段……
您也可以使用此oneliner覆盖。
ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true;
只需将其粘贴到Reference.cs
生成的WCF客户端构造函数中即可
上述就是C#学习教程:如何使用WCF服务临时停止证书错误分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
[System.Diagnostics.DebuggerStepThroughAttribute()] [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0")] public partial class WebQuoteServiceClient : System.ServiceModel.ClientBase, Corp.Legal.Webservices.ServiceReference1.IWebQuoteService { public WebQuoteServiceClient() { ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, errors) => true; }
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1029051.html