简体   繁体   English

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

[英]Using pem certificate and key for authentication in powershell https request

I have certificates in pem format. 我有pem格式的证书。 2 files totally, RSA Public and RSA Private keys. 总共2个文件,RSA公钥和RSA私钥。 I have to use these to make a https request to the server in powershell script. 我必须使用这些命令以Powershell脚本向服务器发出https请求。

I tried adding the certificate using X509Certificates Certificate store. 我尝试使用X509Certificates证书存储添加证书。 But I am not sure how to add the client key certificate( RSA private key ). 但是我不确定如何添加客户端密钥证书(RSA私钥)。 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. hannel。

How can I request with both client certificate and key using powershell? 如何使用Powershell要求客户证书和密钥?

The powershell script that I have written: 我编写的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

Although I was able to make the request using the node.js Node code: 尽管我可以使用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);
});

Thanks. 谢谢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM