基本上我想将命令 curl 翻译为与当前在 linux 服务器中相同的参数,但在 Powershell 中,以便上传文件:
curl -v -T $file -u user:password http://myurl --cacert /opt/keystores/ca_cert.pem
我找到了执行此任务的等效命令:PowerShell 3.0+ 的“Invoke-WebRequest”,但问题是我不知道如何使用 CA 证书文件 (.pem) 调用它,而且我还没有'在互联网上找到任何样本。
谢谢!
最佳答案
当您在 .NET 中建立 TLS
连接时,将根据 RemoteCertificateValidationCallback
function 验证对等证书,由相关 AppDomain 的 ServicePointManager
管理。
大多数关于如何在 PowerShell 中覆盖默认验证的示例都会告诉您只需执行以下操作:
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
不要那样做! - 它会完全绕过验证。
您可以做的是实现适当的回调函数并手动调用链验证。在执行此操作之前,您可以将不在机器或用户证书存储中的证书添加到您可以验证的链列表中:
$callback = {
param(
$sender,
[System.Security.Cryptography.X509Certificates.X509Certificate]$certificate,
[System.Security.Cryptography.X509Certificates.X509Chain]$chain,
[System.Net.Security.SslPolicyErrors]$sslPolicyErrors
)
# No need to retype this long type name
$CertificateType = [System.Security.Cryptography.X509Certificates.X509Certificate2]
# Read the CA cert from file
$CACert = $CertificateType::CreateFromCertFile("C:\path\to\ca.crt") -as $CertificateType
# Add the CA cert from the file to the ExtraStore on the Chain object
$null = $chain.ChainPolicy.ExtraStore.Add($CACert)
# return the result of chain validation
return $chain.Build($certificate)
}
# Assign your delegate to the ServicePointManager callback
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = $callback
# Do your Invoke-WebRequest or WebClient call here
我不知道如何从一个 PEM 文件中读取多个证书到一个证书集合中,所以你必须一个一个地添加每个 ca 证书,抱歉
关于windows - 如何使用 cacert 文件在 Powershell (Invoke-WebRequest) 中执行等效的 cUrl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36722870/