VB .net 接受自签名 SSL 证书

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5998004/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 15:34:48  来源:igfitidea点击:

VB .net Accept Self-Signed SSL certificate

.netvb.netsslssl-certificate

提问by compcentral

I'm searching for a way to validate (or bypass validation for) self-signed SSL certificates using VB .Net. I found code to do this in C# and tried converting it into VB code, but I'm not having any luck.

我正在寻找一种使用 VB .Net 验证(或绕过验证)自签名 SSL 证书的方法。我找到了在 C# 中执行此操作的代码并尝试将其转换为 VB 代码,但我没有任何运气。

Here is the C# code.

这是C# 代码。

Here is what I tried:

这是我尝试过的:

Imports System
Imports System.Net
Imports System.Security.Cryptography.X509Certificates

Public Class clsSSL
    Public Function AcceptAllCertifications(ByVal sender As Object, ByVal certification As System.Security.Cryptography.X509Certificates.X509Certificate, ByVal chain As System.Security.Cryptography.X509Certificates.X509Chain, ByVal sslPolicyErrors As System.Net.Security.SslPolicyErrors) As Boolean
        Return True
    End Function
End Class

Then before the Webrequest I have this line of code which gives me an error.

然后在 Webrequest 之前,我有这行代码,它给了我一个错误。

ServicePointManager.ServerCertificateValidationCallback = New System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications)

The error message is:

错误信息是:

Delegate 'System.Net.Security.RemoteCertificateValidationCallback' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.

回答by SLaks

In VB.Net, you need to write

在VB.Net中,你需要写

ServicePointManager.ServerCertificateValidationCallback = AddressOf AcceptAllCertifications

回答by MrCalvin

One-liner:

单线:

System.Net.ServicePointManager.ServerCertificateValidationCallback = _
  Function(se As Object, _
  cert As System.Security.Cryptography.X509Certificates.X509Certificate, _
  chain As System.Security.Cryptography.X509Certificates.X509Chain, _
  sslerror As System.Net.Security.SslPolicyErrors) True

Credits to Robby Tendean

罗比Tendean

回答by Tim Schmelter

I'm not sure but this should work:

我不确定,但这应该有效:

ServicePointManager.ServerCertificateValidationCallback = _
      New RemoteCertificateValidationCallback(AddressOf AcceptAllCertifications)

http://msdn.microsoft.com/de-de/library/system.net.security.remotecertificatevalidationcallback%28VS.90%29.aspx

http://msdn.microsoft.com/de-de/library/system.net.security.remotecertificatevalidationcallback%28VS.90%29.aspx

回答by vidhya

In VB.Net,

在 VB.Net 中,

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls

solves the less secure apps problem.

解决了不太安全的应用程序问题。