使用自签名证书和基本身份验证的 Powershell Invoke-RestMethod - 任何示例? [英] Powershell Invoke-RestMethod using Self-Signed Certificates and Basic Authentication - Any Examples?

查看:42
本文介绍了使用自签名证书和基本身份验证的 Powershell Invoke-RestMethod - 任何示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 RESTful API 并且使用 Firefox RESTClient 插件一切正常.我可以轻松查询 API.

I'm playing with a RESTful API and using the Firefox RESTClient plugin all is well. I can easily query the API.

但是,当我尝试将相同的 API 调用插入 powershell 时,它不起作用!

However, when I try to plug the same API call into powershell it doesn't work!

我已经尝试了其他各种帖子中的以下代码,这些代码应该可以排除证书问题,但我仍然无法使其正常工作:

I've tried the following code from various other posts which should rule out certificate issues but I still can not get this to work:

    # This nugget should help me to get around any self-signed certificate issues I believe

    $netAssembly =[Reflection.Assembly]::GetAssembly([System.Net.Configuration.SettingsSection])

    if($netAssembly)
    {
        $bindingFlags = [Reflection.BindingFlags] "Static,GetProperty,NonPublic"
        $settingsType = $netAssembly.GetType("System.Net.Configuration.SettingsSectionInternal")

        $instance = $settingsType.InvokeMember("Section", $bindingFlags, $null, $null, @())

        if($instance)
        {
            $bindingFlags = "NonPublic","Instance"
            $useUnsafeHeaderParsingField = $settingsType.GetField("useUnsafeHeaderParsing", $bindingFlags)

            if($useUnsafeHeaderParsingField)
            {
              $useUnsafeHeaderParsingField.SetValue($instance, $true)
            }
        }
    }

    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}

    $headers = @{"AUTHORIZATION"="Basic YWRtaW46Y2xvdWQ="}

    # I exported this certificate from the web browser
    $cert=New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("HPCSA.crt")

    Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admin -Certificate $cert -Headers $headers -Method Get`

我无法使用 Powershell V3 的 Invoke-RestMethod 进行复制,我想知道是否有人可以共享示例代码来访问具有自签名证书并使用基本授权的 HTTPS RESTful API.

I'm not able to replicate this using Powershell V3's Invoke-RestMethod and was wondering if someone could share sample code for accessing a HTTPS restful API that has a self-signed certificate and also use basic authorization.

我收到的错误消息:

PS C:\Users\landg> C:\Users\landg\Documents\Scripts\CSA API\CSA_API_DEMO_take2.ps1
Invoke-RestMethod : Unable to connect to the remote server
At C:\Users\landg\Documents\Scripts\CSA API\CSA_API_DEMO_take2.ps1:31 char:1
+ Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

推荐答案

我的解决方案的来源

使用它让我进入实际的服务器......现在有一个 HTTP 500 错误,但这个错误是另一天.

Using this gets me onto the actual server....now with a HTTP 500 error but this error is for another day.

这是我的工作"片段:

     add-type @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;

            public class IDontCarePolicy : ICertificatePolicy {
            public IDontCarePolicy() {}
            public bool CheckValidationResult(
                ServicePoint sPoint, X509Certificate cert,
                WebRequest wRequest, int certProb) {
                return true;
            }
        }
    "@
    [System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy 

    Invoke-RestMethod -Uri https://192.168.5.3:8444/csa/rest/login/CSA-Provider/admin -Headers @{"AUTHORIZATION"="Basic YWRtaW46Y2xvdWQ="} -Method Get

希望这能帮助其他人度过几个令人沮丧的小时:)

Hopefully this will help someone else from several frustrating hours :)

这篇关于使用自签名证书和基本身份验证的 Powershell Invoke-RestMethod - 任何示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆