asp.net - 如果.NET中证书已过期,如何调用https asmx Web服务

标签 asp.net asp.net-mvc web-services asmx security

使用 Visual Studio 从 MVC2 Controller 生成的代码(使用下面的代码)调用 Asmx 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(); 
    } 
} 

}

最佳答案

来自James blog :

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.

所以,他做了以下事情:

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

关于asp.net - 如果.NET中证书已过期,如何调用https asmx Web服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7702402/

相关文章:

c# - 如何将 Model.Tag(具有 ID 和名称)转换为 "List of"名称?

java - 如何防止 Web 服务 API 中的并发?

.net - 'System.InvalidOperationException : Request format is invalid: multipart/form-data' error when posting image from iphone to . NET 网络服务

java - 如何使用放心调用web服务

asp.net - ASP.NET 是否将所有程序集从 bin 加载到 AppDomain 中?

ASP.NET - 使用 WCF Web 服务绑定(bind)和 AD 组时出现 IIS7 部署错误 500 24 50

mysql - MySQL 的 ASP.NET MVC 3 成员资格

c# - c#razor 字符串与带有尾部斜杠的 html 字符串的连接

c# - 动态字段名称

html - 具有不透明度值的悬停背景和具有不同不透明度值的悬停按钮