Unexpected error occurred running a simple unauthorized Rest query

前端 未结 3 1265
天命终不由人
天命终不由人 2021-01-31 08:33

I have a rest endpoint that doesn\'t perform an authentication check. I can run a simple curl commmand from Linux:

curl -k https://application/api/about
<         


        
相关标签:
3条回答
  • Using:

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    

    Taken from Powershell 3.0 Invoke-WebRequest HTTPS Fails on All Requests.

    0 讨论(0)
  • 2021-01-31 08:51

    in my case the TLS trick did not work, this seems to be a bug in powershell. you need to add the callback using .net code instead of a scriptblock.

    #C# class to create callback
    $code = @"
    public class SSLHandler
    {
        public static System.Net.Security.RemoteCertificateValidationCallback GetSSLHandler()
        {
    
            return new System.Net.Security.RemoteCertificateValidationCallback((sender, certificate, chain, policyErrors) => { return true; });
        }
    
    }
    "@
    
    #compile the class
    Add-Type -TypeDefinition $code
    
    #disable checks using new class
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = [SSLHandler]::GetSSLHandler()
    #do the request
    try
    {
        invoke-WebRequest -Uri myurl -UseBasicParsing
    } catch {
        # do something
    } finally {
       #enable checks again
       [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
    }
    
    0 讨论(0)
  • 2021-01-31 09:11

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

    Works in Windows server 2016

    Major Minor Build Revision


    5 1 17763 1007

    0 讨论(0)
提交回复
热议问题