我想从托管远程证书的Windows服务器调用远程Rest Web服务。 我已从远程服务器导出证书并将其添加到Windwos商店。 (/个人/ myCert)
我想在Invoke-RestMethod PowerShell命令中使用它。 下面是我试过的代码
# Variables
$Remote_Uri = "https://remote.example.com/service/search"
$Remote_CertificateName = "myCert"
$Remote_ApiKey = "oisdjfSOEDJFKQDfSDKFjsQDKFJ"
$Remote_ContentType = "application/json"
$LocalArtifactPath = "C:\RemoteObjects.json"
# Get Certificate
$Remote_CertificateThumbprint = (Get-ChildItem -Path Cert:\LocalMachine\My | Where-Object {$_.Subject -match $Remote_CertificateName}).Thumbprint;
$Certificate = Get-ChildItem -Path Cert:\LocalMachine\My\$Remote_CertificateThumbprint
# Basic Encoding
$encoding = [System.Text.Encoding]::ASCII.GetBytes($Certificate)
$encodedString = [System.Convert]::ToBase64String($encoding)
$BasicAuth = "Basic " + $encodedString
# Set Headers
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("Authorization", $BasicAuth)
$Headers.Add("api", $Remote_ApiKey)
$Headers.Add("Content-Type", $Remote_ContentType)
# Self-signed certificate
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true }
# Call Rest Service
Invoke-RestMethod -Method Get -Uri $Remote_Uri -OutFile $LocalArtifactPath -Headers $Headers
Invoke-RestMethod -Method Get -Uri $Remote_Uri -OutFile $LocalArtifactPath -Certificate $Certificate
Invoke-RestMethod -Method Get -Uri $Remote_Uri -OutFile $LocalArtifactPath -CertificateThumbprint $Remote_CertificateThumbprint
# Self-signed certificate off
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
Invoke-RestMethod
命令的三行分别失败:
我正在使用@{"AUTHORIZATION"="Basic Base64Encode(user:pass)"}
进行其余调用,所以我可以告诉服务正在回答,但我不想使用user:传入我的脚本。
我想使用我添加到Windows应用商店的证书。 我想知道两件事:
感谢您的帮助
答案 0 :(得分:0)
在我的脚本中添加此[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
可以解决“基础连接已关闭”的问题。
在您的系统中启用此交叉检查IIS之前。