Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.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
Powershell http post,带有用于身份验证的.cer_Powershell - Fatal编程技术网

Powershell http post,带有用于身份验证的.cer

Powershell http post,带有用于身份验证的.cer,powershell,Powershell,对PowerShell很陌生,希望有人能给我指出正确的方向 我需要执行POST请求,并在POST请求期间向其传递本地存储的证书x509,以进行身份验证 实现这一目标的最佳方式是什么?我发现有很多例子可以在.net/C中执行此任务,但我没有发现任何东西可以在PowerShell中完成此任务 这是我的POST请求代码,我想再次指向本地存储的证书C:\code\cert.crt,并在web事务期间传递它 $url = "https://myUrl/uploadTester" $data = '{"da

对PowerShell很陌生,希望有人能给我指出正确的方向

我需要执行POST请求,并在POST请求期间向其传递本地存储的证书x509,以进行身份验证

实现这一目标的最佳方式是什么?我发现有很多例子可以在.net/C中执行此任务,但我没有发现任何东西可以在PowerShell中完成此任务

这是我的POST请求代码,我想再次指向本地存储的证书C:\code\cert.crt,并在web事务期间传递它

$url = "https://myUrl/uploadTester"
$data = '{"data": "988309487577839444"}'
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$b = [System.Text.Encoding]::ASCII.GetBytes($data)
$web = [System.Net.WebRequest]::Create($url)
$web.Method = "POST"
$web.ContentLength = $b.Length
$web.ContentType = "application/x-www-form-urlencoded"
$stream = $web.GetRequestStream()
$stream.Write($b,0,$b.Length)
$stream.close()

$reader = New-Object System.IO.Streamreader -ArgumentList $web.GetResponse().GetResponseStream()
$reader.ReadToEnd()
$reader.Close()

感谢您在advanced中提供的所有帮助

将C转换为PowerShell非常容易。尝试一下:

[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
$cert = [System.Security.Cryptography.X509Certificates.X509Certificate2]::CreateFromCertFile("C:\Users\Andy\Desktop\Test.cer")
$web = [System.Net.WebRequest]::Create($url)
$web.ClientCertificates.Add($Cert)
我改编自:

看起来密钥是ClientCertificates属性


谢谢你的快速回复,安迪。我有一个问题:谢谢你的快速回复,安迪。我确实有一个关于代码的问题。我是否仍需要保留行ServerCertificateValidationCallback={$true}。这不是完全忽略所有证书检查吗?或者,我提供证书的事实是使用Create$url进行实际的证书验证检查吗?@user1048209我认为ServerCertificateValidationCallback={$true}用于服务器端证书验证,而不是发送到服务器的客户端证书。