如何捕获“远程证书无效"?没有抓住一切的例外 [英] How to catch a "The remote certificate is invalid" exception without catching everything

查看:77
本文介绍了如何捕获“远程证书无效"?没有抓住一切的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个处理发送电子邮件的例程(实际上是类).

我有一个条件例程,该例程将忽略所有证书错误(它基本上只接受所有有效的证书),但是当该例程关闭(由用户关闭)时,我希望能够捕获这些异常并给出一个漂亮的简单错误消息,而不是现在返回的两页文本.

如果不是那个错误,那么我将需要所有错误文本以弄清楚发生了什么并为客户端修复它,但是可以预料的是,此错误不需要所有文本.

我想捕捉的异常是:

"System.Security.Authentication.AuthenticationException:根据验证过程,远程证书无效"

我怎样才能做到这一点?如果这是一个非常简单的问题,请原谅我...我似乎找不到一个例子.

我已经在使用Try Catch End Try了...所以我知道我的测试以查看是否发生了这种特定的异常,它将进入代码的Catch部分中...我只是不知道该测试什么才能测试我的异常变量反对让我针对这种情况适当地调整错误消息,而又不会因为我目前已对其他异常进行编码而阻止其他异常的处理.

谢谢.

I have a routine (class actually) that handles sending emails.

I have a conditional routine that I put in place that will ignore all Certificate errors (it basically just accepts all certificates as valid) but when that routine is turned off (by the user) I want to be able to catch those exceptions and give a nice simple error message rather than the 2 pages of text that get sent back right now.

If it''s not that error then I''ll need all the error text in order to figure out what''s happening and fix it for the client, but this error, being expected, doesn''t need all the text.

The Exception I''m trying to catch is:

"System.Security.Authentication.AuthenticationException: The remote certificate is invalid according to the validation procedure"

How can I do that? Forgive me if this is a pretty simple question ... I just can''t seem to find an example.

I am using a Try Catch End Try already ... so I know my test to see if this specific exception has occurred will go into the Catch portion of the code ... I just don''t know what to test my exception variable against to allow me to adjust my error message appropriately for this single situation without blocking the other exceptions from being handled as I''ve currently got them coded.

Thanks.

推荐答案

您尚未显示代码,所以这只是一个猜测.但必须要您尝试:
You haven''t shown your code so this is just a guess. but have to you tried:
Try
    ....
Catch ex As AuthenticationException
    ....
End Try


这篇关于如何捕获“远程证书无效"?没有抓住一切的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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