【问题标题】:How to disable remote certificate validation using .NET SSLStreams如何使用 .NET SSLStreams 禁用远程证书验证
【发布时间】:2021-10-31 16:53:46
【问题描述】:

在 .NET SSLStream 类中,调用“AuthenticateAsClient”方法时,必须传递“targetHost”,基于此验证远程证书。

如果 targetHost 设置为“*”,则不会按照 msdn link 中的说明验证远程证书

现在我们是否可以将“targetHost”设置为有效值,但仍希望禁用远程证书验证。

【问题讨论】:

标签: c# asp.net .net asp.net-core .net-core


【解决方案1】:

您需要使用证书验证回调,以便决定何时接受或忽略远程证书,在您想要忽略远程证书并接受它而不考虑错误的用例中。

使用SSLStream 有两种方法可以实现它:

  1. 使用具有RemoteCertificateValidationCallback 参数的constructor overload。您在the official documentation of the RemoteCertificateValidationCallback 中有一个代码示例
  2. 使用具有 SslClientAuthenticationOptions 参数的 AuthenticateAsClient(SslClientAuthenticationOptions) overload 和可用于忽略远程错误/无效证书的 RemoteCertificateValidationCallback 属性。

自定义回调函数的代码与此类似:

public static bool ValidateServerCertificate(
      object sender,
      X509Certificate certificate,
      X509Chain chain,
      SslPolicyErrors sslPolicyErrors)
{
   // We want to ignore remote certificate errors
   // Warning: In a normal scenario we would check the sslPolicyErrors argument instead.
   return true;
}

【讨论】:

    猜你喜欢
    • 2020-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多