如何调用HTTPS ASMX web服务,如果证书中的.NET已过期 [英] How to call https asmx web service if certificate has expired in .NET

查看:441
本文介绍了如何调用HTTPS ASMX web服务,如果证书中的.NET已过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASMX Web服务使用Visual Studio被称为使用下面code产生code从MVC2控制器。

Asmx web service is called using Visual Studio generated code from MVC2 controller using code below.

由于Web服务证书已过期方法调用抛出异常。如何使Web服务仍然可以使用解决这个问题?

Method call throws exception since web service certificate has expired. How to fix this so that web service can still used?

使用.NET 3.5和MVC2。

Using .NET 3.5 and MVC2.

public class AsmxController : Controller 
{ 
    public ActionResult Index() 
    { 
        var cl = new store2.CommerceSoapClient(); 

        //    System.ServiceModel.Security.SecurityNegotiationException was unhandled by user code 
        //Message=Could not establish trust relationship for the SSL/TLS secure channel with authority 'asmxwebservice.com'. 
        var vl = cl.GetVendorList(  AsmxService.LicenseHeader() , 
            new AsmxService.GetVendorListRequest()); 
        return View(); 
    } 
} 

}

推荐答案

从<一个href=\"http://blog.jameshiggs.com/2008/05/01/c-how-to-accept-an-invalid-ssl-certificate-programmatically/\"相对=nofollow>詹姆斯博客:

所以,进行测试,我们需要找到一种方法来绕过证书
  验证。事实证明,您需要提供
   RemoteCertificateValidationCallback 委派,并将其附加到
   ServicePointManager.ServerCertificateValidationCallback 。什么是不
  显而易见的是,如果两个线程都在争相设置此发生了什么
  属性不同的值,因为它是一个静态属性。反射
  表明属性设置方法不会做任何事情看中了,所以
  你可以很容易地进入比赛状态。

So, for testing, we needed to find a way to bypass the certificate validation. It turns out that you need to provide a RemoteCertificateValidationCallback delegate and attach it to ServicePointManager.ServerCertificateValidationCallback. What’s not clear is what happens if two threads are competing to set this property to different values, since it’s a static property. Reflector suggests that the property set method doesn’t do anything fancy, so you could easily get into a race condition.

所以,他做了以下内容:

so, he does the following:

// 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 certificate,    X509Chain chain,    SslPolicyErrors policyErrors)
{
    if (Convert.ToBoolean(ConfigurationManager.AppSettings["IgnoreSslErrors"]))
    {
        // allow any old dodgy certificate...
        return true;
    }
    else
    {
        return policyErrors == SslPolicyErrors.None;
    }
}

这篇关于如何调用HTTPS ASMX web服务,如果证书中的.NET已过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆