我写一个测试,测试服务,我部署,绕过SSL证书检查我用下面的代码片段实现的SSL覆盖:
public static void SSLValidationOverride()
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(OnValidationCallback);
}
private static bool OnValidationCallback(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors errors)
{
if (cert.subject == MyCertSubject)
return true;
else
return false;
}
Run Code Online (Sandbox Code Playgroud)
现在,我在代码中使用SSL来调用另一个Web服务,并希望在调用之前切换到默认的SSL检查.什么是最好的方法.MS的帮助表示ServicePointManager.SecurityProtocol的默认值为null(http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.securityprotocol.aspx).将其设置为null会切换到默认的ssl验证,还有其他方法可以执行此操作.
小智 6
在开发我创建的诊断工具期间,我遇到了类似的问题.我创建了一个Windows服务,它向URL列表发出请求.对于每个网址,我通过设置以下方式提出没有证书验证的请求:
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
Run Code Online (Sandbox Code Playgroud)
然后使用相同的url我还尝试通过将属性重置为null来提出证书验证请求,该属性应提供默认功能:
ServicePointManager.ServerCertificateValidationCallback = null;
Run Code Online (Sandbox Code Playgroud)
这有意想不到的结果,因为我的请求仍未在没有证书验证的情况下进行,就像忽略了重置一样.调试后,我发现如果我在设置后等待足够长的时间
ServicePointManager.ServerCertificateValidationCallback = null;
Run Code Online (Sandbox Code Playgroud)
然后它实际上被重置,我的请求是使用默认证书验证.所以我在做出请求之前只是睡了30秒:
ServicePointManager.ServerCertificateValidationCallback = null;
System.Threading.Thread.Sleep(30000);
//Make request here
Run Code Online (Sandbox Code Playgroud)
我不明白为什么会这样,但它确实有,我相信有更好的方法,如果你知道如何,请告诉我.
我最终将 ServicePointManager.SecurityProtocol 设置为 null 并且工作正常。尽管我仍然对更好的方法持开放态度
public static void SSLValidationRestore()
{
ServicePointManager.ServerCertificateValidationCallback = null;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3318 次 |
| 最近记录: |