根据验证程序,远程证书无效

时间:2017-09-10 13:57:46

标签: c# wcf ssl https

我想调用服务,你可以看到:

public string ServiceHostName = "https://122.333.ir/InquiryService.svc";
        public InquiryService()
        {
            //ClientRequest = new WebClient();
            ms = new MemoryStream();
        }

        public Inquiryview Inquiry(string Plaque)
        {
            string result = "";
            using (WebClient ClientRequest = new WebClient())
            {
                ClientRequest.Headers["Content-type"] = "application/json";
                ClientRequest.Encoding = System.Text.Encoding.UTF8;
                NetworkCredential credential1 = new NetworkCredential("1", "1");
                ClientRequest.Credentials = credential1;
                //result = ClientRequest.DownloadString(ServiceHostName + "/" + "2012-12-28" + "/" + "61" + "-" + "921" + "ج" + "25");
                 result = ClientRequest.DownloadString(ServiceHostName + "/Inquiry/2012-12-28" + "/" + Plaque);
            }

            var javascriptserializer = new JavaScriptSerializer();
            return javascriptserializer.Deserialize<Inquiryview>(result);
        }

但是当我打电话给服务时,我得到了这个错误:

Exception information: 
    Exception type: AuthenticationException 
    Exception message: The remote certificate is invalid according to the validation procedure.
   at System.Net.Security.SslState.StartSendAuthResetSignal(ProtocolToken message, AsyncProtocolRequest asyncRequest, Exception exception)

我的服务证书是一个自签名,所以我下载了证书并将其添加到受信任的根证书中,如您所见,但我收到同样的错误:

enter image description here

2 个答案:

答案 0 :(得分:1)

一种选择是实施此callback

Service​Point​Manager.​Server​Certificate​Validation​Callback

这将使您能够获得有关完全认证失败的更多详细信息。

您可以查看SslPolicyErrors枚举以查看确切的失败原因

public enum SslPolicyErrors
{
    None                          = 0x0,
    RemoteCertificateNotAvailable = 0x1,
    RemoteCertificateNameMismatch = 0x2,
    RemoteCertificateChainErrors  = 0x4
}

答案 1 :(得分:0)

我的最终解决方案,但不推荐

static void Main(string[] args)
        {

            System.Net.ServicePointManager.ServerCertificateValidationCallback +=
    (se, cert, chain, sslerror) =>
    {
        return true;
    };

            string ServiceHostName = "https://111.333.ir/InquiryService.svc";
        string result = "";
            using (WebClient ClientRequest = new WebClient())
            {
                ClientRequest.Headers["Content-type"] = "application/json";
                ClientRequest.Encoding = System.Text.Encoding.UTF8;
                NetworkCredential credential1 = new NetworkCredential("1", "1");
                ClientRequest.Credentials = credential1;
                //result = ClientRequest.DownloadString(ServiceHostName + "/" + "2012-12-28" + "/" + "61" + "-" + "921" + "ج" + "25");
                result = ClientRequest.DownloadString(ServiceHostName + "/Inquiry/2012-12-28" + "/" + "11-365ب12");
            }

           // var javascriptserializer = new JavaScriptSerializer();
        }