好吧,我正在使用一个 Web 服务,该服务的证书未 100% 正确设置,该证书是为域 *.domain1.com 设置的,并且 api 位于soap.shop.domain1.com/SOAP 现在我无法连接到此webservice,然后我得到一个 WebException“无法为 SSL/TLS 安全通道建立 trush 关系。-->
根据验证程序,远程证书无效。
现在我的问题是有什么方法可以绕过这个检查我使用普通的Web参考(2.0)而不是服务参考..
对于那些无法确定从哪里开始回答的人来说,这可能并不明显。上面的海报是正确的,但没有明确说明如何处理给定的代码。
假设您有一个类需要使用证书调用 Web 服务。
这是我完成的解决方案:
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;}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)