Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在powershell https请求中使用pem证书和密钥进行身份验证_Node.js_Powershell_Https_X509certificate_Pem - Fatal编程技术网

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

Node.js 在powershell https请求中使用pem证书和密钥进行身份验证,node.js,powershell,https,x509certificate,pem,Node.js,Powershell,Https,X509certificate,Pem,我有pem格式的证书。共2个文件,RSA公钥和RSA私钥。 我必须使用这些脚本在powershell脚本中向服务器发出https请求 我尝试使用X509Certificates证书存储添加证书。但是我不知道如何添加客户机密钥证书(RSA私钥)。 我尝试仅使用证书,但出现以下错误: Exception Message: The underlying connection was closed: Could not establish trust relationship for the SSL/T

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

我尝试使用X509Certificates证书存储添加证书。但是我不知道如何添加客户机密钥证书(RSA私钥)。 我尝试仅使用证书,但出现以下错误:

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

如何使用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
虽然我能够使用节点.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);
});

谢谢。

将PEM文件转换为PKCS12对我很有效。

您是否将整个PEM导入了商店?certmgr.msc是否将证书显示为具有关联私钥?我认为您需要将这些文件转换为PKCS#12格式,然后才能使用主键作为证书导入。IIRC Windows在运行其SSL引擎时不允许将密钥存储为文件。是的,您必须将公钥和关联的私钥文件合并到单个PKCS#12容器中,然后将其安装到Windows证书存储。我刚刚将公钥导入证书存储。我将尝试将public和private合并到PKCS12中,并尝试在证书存储中安装。然后尝试点击服务器。谢谢。@Vesper:我想我的系统中有很多证书。如何知道使用哪一种?我认为你应该通过指纹过滤,这是最快、最可靠的方法。您有一个循环存储,选择与PEM中证书指纹匹配的一个,然后存储在您的
$certToUse
中。在脚本中存储指纹是可以接受的,只需记录在证书续订的情况下,您需要更新脚本。