2011-10-09 27 views
3

使用Visual Studio生成的代碼從MVC2控制器使用以下代碼調用Asmx Web服務。如果證書在.NET中過期,則如何調用https asmx Web服務

自Web服務證書過期以來,方法調用會拋出異常。如何解決這個問題,以便Web服務仍然可以使用?

使用.NET 3.5和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(); 
    } 
} 

}

回答

3

James blog

因此,進行測試,我們需要找到一種方法來繞過證書 驗證。事實證明,您需要提供 RemoteCertificateValidationCallback代表並將其附加到 ServicePointManager.ServerCertificateValidationCallback。如果兩個線程競爭將此 屬性設置爲不同的值,因爲它是一個靜態屬性,什麼不是 清除是什麼。反射器 表明屬性設置方法不會做任何事情,所以 你很容易進入競爭狀態。

如此,他執行以下操作:

// 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; 
    } 
}