Search code examples
vb.netsslhttpsrestsharp

How To Ignore Certificate Validation When POSTing via RestSharp in VB.Net


I'm using the below code to try and access an API using RestSharp in VB.Net:

    Dim myClient As New RestClient(*https api website url*)
    Dim myRequest As New RestRequest(Method.POST)
    myRequest.AddHeader("Content-Type", "application/json")
    myRequest.AddCookie("JSESSIONID", "568E16300DB90F7CEC5A7882C66D3684")
    myRequest.AddCookie("sessionExpiry", "3600000") ' 1 Hour
    myRequest.AddParameter("undefined", "{\""username\"":\""*myUsername*\"",\""password\"":\""*myPassword*\"",\""clientInformation\"":{\""userAgent\"":\""Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36\"",\""height\"":1440,\""width\"":2560,\""os\"":\""Win32\""},\""application\"":\""p4app\""}", ParameterType.RequestBody)
    Dim myResponse As IRestResponse
    myResponse = myClient.Execute(myRequest)
    MsgBox(myResponse.Content)

it keeps giving me the error '"message":"Login Failed - Invalid login request"'

I've been told I need to ignore the SSL certificate but I can't seem to find a way to do that using RestSharp. Any ideas?


Solution

  • I don't know what RestSharp is using underneath, but it might work like this:

    Imports:

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

    Register a certificate validator:

        Dim myHandler As RemoteCertificateValidationCallback =
            Function(sender As Object, certificate As X509Certificate, chain As X509Chain, sslPolicyErrors As SslPolicyErrors) As Boolean
                Return True
            End Function
    
        ServicePointManager.ServerCertificateValidationCallback = myHandler