Search code examples
c#vb.netssltranslationself-signed

The remote certificate is invalid according to the validation procedure with self-signed certificate


I'm getting the error `The remote certificate is invalid according to the validation procedure when requesting an URL on my local development machine.

I already looked here.

But I can't find the VB.NET code for this C# code:

ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;

OR

// Put this somewhere that is only once - like an initialization method
ServicePointManager.ServerCertificateValidationCallback += new RemoteCertificateValidationCallback(ValidateCertificate);
...

static bool ValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
   return true;
}

Can someone help me with the translation of this code? I tried the translators converter.telerik.com and carlosag.net but those fail.


Solution

  • Rather than just translate let's first determine what this line does

     ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true;
    

    The MSDN docs says that ServerCertificateValidationCallback is a property of type RemoteCertificateValidationCallback

    RemoteCertificateValidationCallback is a delegate with this signature

    'Declaration
    Public Delegate Function RemoteCertificateValidationCallback ( _
        sender As Object, _
        certificate As X509Certificate, _
        chain As X509Chain, _
        sslPolicyErrors As SslPolicyErrors _
    ) As Boolean
    

    This (o, c, ch, er) => true; is a lamba expression with the signature RemoteCertificateValidationCallback and always evaluates true.

    To do the same in VB.NET it's

    ServicePointManager.ServerCertificateValidationCallback = Function(o,c,ch,er) (true)
    

    This article will help will you with the second part, but it's the same idea.