我有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);
});
感谢。
答案 0 :(得分:0)
将PEM文件转换为PKCS12对我有用。