vb.net .net WebService,绕过ssl验证!

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/721472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:09:22  来源:igfitidea点击:

.net WebService, bypass ssl validation!

vb.netweb-servicesssl

提问by Peter

Well im working agains a webservice that has a certificate that is not 100% correctly setup the certificate is setup for the domain *.domain1.com and the api is located at soap.shop.domain1.com/SOAP now i cant connect to this webservice as i then get a WebException "Could Not establish trush relationship for the SSL/TLS secure channel. --> The remote certificate is invalid according to the validation procedure.

好吧,我再次使用具有未 100% 正确设置的证书的 Web 服务为域 *.domain1.com 设置证书,并且 api 位于 soap.shop.domain1.com/SOAP 现在我无法连接到此webservice 因为我然后得到一个 WebException “无法为 SSL/TLS 安全通道建立 trush 关系。--> 根据验证程序,远程证书无效。

Now my question is there any way to bypass this check i use a normal Web Reference (2.0) not a Service Reference..

现在我的问题是有什么方法可以绕过这个检查,我使用的是普通的 Web 参考(2.0)而不是服务参考。

回答by p.campbell

For those who can't determine where to start with this answer, it may not be obvious. The posters above are getting it right, but it wasn't apparent upfront on what to do with the given code.

对于那些无法确定从哪里开始这个答案的人来说,这可能并不明显。上面的海报是正确的,但在如何处理给定代码方面并不清楚。

Let's say you have a class somewhere that needs to call a web service with a certificate.

假设您在某个地方有一个类需要使用证书调用 Web 服务。

Here's my finished solution:

这是我完成的解决方案:

public class MyClass
{

    public  bool TrustAllCertificatesCallback(object sender, X509Certificate cert,
                                                X509Chain chain, SslPolicyErrors errors)
    {
        return true;
    }

    public string CallSomeWebService(string someParam)
    {
        try
        { 
            ServicePointManager.ServerCertificateValidationCallback = TrustAllCertificatesCallback;


            RemoteWebService ws = new RemoteWebService();

            //add the client cert to the web service call.
            ws.ClientCertificates.Add(GetMyCert());

            //call the web service
            string response = ws.SomeMethod(someParam);

            return response.ToString();
        }
        catch (Exception ex)
        {throw;}
    }

    public X509Certificate GetMyCert()
    {
        try
        {
            string certPath = @"C:\MyCerts\MyCert.cer";
            var cert = X509Certificate.CreateFromCertFile(certPath);
            return cert;
        }
        catch (Exception ex)
        {throw;}
    }
}

回答by Chris Ballance

Yes, you can use the following to have ASP.NET ignore the certificate warnings:

是的,您可以使用以下内容让 ASP.NET 忽略证书警告:

using System;
using System.Net;
using System.Security.Cryptography.X509Certificates;

namespace YourNamespace
    public class TrustAllCertificatePolicy : System.Net.ICertificatePolicy
    {
        public TrustAllCertificatePolicy() {}

        public bool CheckValidationResult(ServicePoint sp, X509Certificate cert,WebRequest req, int problem)
        {
            return true;
        }
    }
}

回答by Mark Brackett

System.Net.ServicePointManager.ServerCertificateValidationCallback = _
   Function(a, b, c, d) True

回答by Sebastian Castaldi

pick you flavor..

挑你的口味..

lambda expresions

拉姆达表达式

            //Trust all certificates
            System.Net.ServicePointManager.ServerCertificateValidationCallback =
                ((sender, certificate, chain, sslPolicyErrors) => true);

            // trust sender (more secure)
            System.Net.ServicePointManager.ServerCertificateValidationCallback
                = ((sender, cert, chain, errors) => cert.Subject.Contains("YourServerName"));

or plain clode (better for testing)

或普通clode(更适​​合测试)

            // validate cert
            // allows for validation of SSL conversations
            ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateRemoteCertificate);

    // callback used to validate the certificate in an SSL conversation
    private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors policyErrors)
    {
        bool result = false;
        if (cert.Subject.ToUpper().Contains("YourServerName"))
        {
            result = true;
        }

        return result;
    }

回答by DCNYAM

You need to handle the event that validates the certificate and just set it to always return true. See the following post for details:

您需要处理验证证书的事件,并将其设置为始终返回 true。有关详细信息,请参阅以下帖子:

http://8r13n.wordpress.com/2007/07/24/bypassing-certificate-validation-in-net/

http://8r13n.wordpress.com/2007/07/24/bypassing-certificate-validation-in-net/