I have certificates in pem format. 2 files totally, RSA Public and RSA Private keys. I have to use these to make a https request to the server in powershell script.
I tried adding the certificate using X509Certificates Certificate store. But I am not sure how to add the client key certificate( RSA private key ). I tried with just the certificate, but I get this error:
Exception Message: The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure c
hannel.
How can I request with both client certificate and key using powershell?
The powershell script that I have written:
$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
Although I was able to make the request using the node.js Node code:
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);
});
Thanks.
via sand
No comments:
Post a Comment