首页 文章

在powershell https请求中使用pem证书和密钥进行身份验证

提问于
浏览
0

我有pem格式的证书 . 共2个文件,RSA Public和RSA私钥 . 我必须使用这些来在powershell脚本中向服务器发出https请求 .

我尝试使用 X509Certificates 证书库添加证书 . 但我不知道如何添加客户端密钥证书(RSA私钥) . 我只尝试了证书,但是我收到了这个错误:

Exception Message: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure c

hannel .

如何使用PowerShell请求客户端证书和密钥?

我写的powershell脚本:

$method = "GET"
# Create a dictionary object that allows header storage in Rest call
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("X-Qlik-Xrfkey",$xrfKey)
$headers.Add("X-Qlik-User", "***")


#Get a selection object for all inactive users
$path = "/qrs/app?xrfkey=$xrfKey"
$theCommand = $senseServerHostName + "/qrs" + $path

$ns = "System.Security.Cryptography.X509Certificates"
$store = New-Object "$ns.X509Store"("My","CurrentUser")
#$store = New-Object System.Security.Cryptography.X509Certificates.X509Store("My", "CurrentUser")
$store.Open("ReadOnly")
#$certs = $store.Certificates.Find("FindByExtension", $certExtension, $false)
#"$store.Certificates"
    ForEach($cert in $store.Certificates)
    {

        $certToUse = $cert
    }
    "$certToUse"
$response = Invoke-RestMethod $theCommand -Headers $headers -Method $method -Certificate $certToUse

虽然我能够使用 node.js 节点代码发出请求:

var https = require('https');    
var fs = require('fs');    
var options = {    
rejectUnauthorized: false,    
hostname: '****',
method: 'GET',
path: '/qrs/app?xrfkey=****',
headers: {
//'Accept': 'application/json',
'x-qlik-xrfkey' : '****',
'X-Qlik-User' : '****'
},
key: fs.readFileSync("C:\\client_key.pem"),
cert: fs.readFileSync("C:\\client.pem")

};
https.get(options, function(res) {
console.log("Got response: " + res.statusCode);
res.on("data", function(chunk) {
console.log("BODY: " + chunk);
});
}).on('error', function(e) {
console.log("Got error: " + e.message);
});

谢谢 .

1 回答

  • 0

    PEM文件到PKCS12的转换对我有用 .

相关问题