带有 .cer 的 Powershell http 帖子用于身份验证 [英] Powershell http post with .cer for auth

查看:36
本文介绍了带有 .cer 的 Powershell http 帖子用于身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PowerShell 新手,希望有人能指出我正确的方向.

Pretty new to PowerShell and hoping someone can point me in the right direction.

我需要执行 POST 请求,并且必须在 POST 请求期间将本地存储的证书 (x509) 传递给它以进行身份​​验证.

I need to perform a POST request and have to pass it a locally stored cert (x509) during the POST request, for authentication.

实现这一目标的最佳方式或方法是什么?我找到了很多可以在 .net/C# 中执行此任务的示例,但我没有找到任何可以在 PowerShell 中完成此任务的示例.

What is the best way or way to accomplish this? I've found plenty of example to be able to perform this task in .net/C# but I am not finding anything that will accomplish this task in PowerShell.

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

Here is my POST request code, again I would like to point to a cert stored locally "C:\code\cert.crt" and pass it during the web transaction.

$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()

感谢所有的高级帮助.

推荐答案

将 C# 转换为 PowerShell 非常容易.试试这个:

It's pretty easy to convert C# to PowerShell. Give this a try:

[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)

我改编自:http://support.microsoft.com/kb/895971

看起来关键是 ClientCertificates 属性.

Looks like the key is the ClientCertificates property.

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.clientcertificates.aspx

这篇关于带有 .cer 的 Powershell http 帖子用于身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆