Powershell使用cookie进行身份验证

时间:2017-02-23 19:29:39

标签: bash powershell cookies

我正在尝试将这两个(工作)curl命令转换为powershell。 如何使用Invoke-WebRequest保存cookie?

获取会话Cookie bash

curl -k \ 
--cookie-jar cookie \
-H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "id": 1, "method": "login", "params": {"username": "bob", "password": "correct-horse-battery-staple"}}' \
https://foo.example.com/api/session

获取会话Cookie powershell

$data = @{}
$data.jsonrpc = '2.0'
$data.id = '1'
$data.method = 'login'
$data.params = @{}
$data.params.username = 'bob'
$data.params.password = 'correct-horse-battery-staple'  
$url = "https://foo.example.com/api/session"
$webrequest = Invoke-WebRequest -Method POST `
-ContentType 'application/json' `
-Body $data `
-Uri $url `
-SessionVariable websession `
-UseBasicParsing
$cookies = $websession.Cookies.GetCookies($url)
Write-Host "$($cookies[0].name) = $($cookies[0].value)"

获取版本bash

curl -k \
--cookie cookie \
-H 'Content-Type: application/json' \
-d '{"jsonrpc": "2.0", "id": 2, "method": "version"}' \
https://foo.example.com/api/about

获取版本powershell

$data = @{}
$data.jsonrpc = '2.0'
$data.id = '2'
$data.method = 'version'
$url = "https://foo.example.com/api/about"
Invoke-WebRequest -Method POST `
-ContentType 'application/json' `
-Body $data `
-Uri $url `
-WebSession $websession `
-UseBasicParsing

第二个命令没有正确传入cookie并返回错误'Decoding failed'

StatusCode        : 200
StatusDescription : OK
Content           : {"error":{"code":-32000,"message":"Decoding failed: Syntax error","data":null},"id":null}
RawContent        : HTTP/1.1 200 OK
                    Pragma: no-cache
                    Keep-Alive: timeout=5, max=100
                    Connection: Keep-Alive
                    Content-Length: 89
                    Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
                    Content-Ty...
Forms             :
Headers           : {[Pragma, no-cache], [Keep-Alive, timeout=5, max=100], [Connection, Keep-Alive], [Content-Length, 89]...}
Images            : {}
InputFields       : {}
Links             : {}
ParsedHtml        :
RawContentLength  : 89

为什么curl成功通过cookie进行身份验证,但是PowerShell错误?

查看变量$ websession

$websession

Headers               : {}
Cookies               : System.Net.CookieContainer
UseDefaultCredentials : False
Credentials           :
Certificates          :
UserAgent             : Mozilla/5.0 (Windows NT; Windows NT 6.3; en-US)
                        WindowsPowerShell/5.0.10586.117
Proxy                 :
MaximumRedirection    : -1

1 个答案:

答案 0 :(得分:2)

Found the solution here

$ data变量是一个Object,而api需要json

 $data.gettype()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Hashtable                                System.Object

解决方案是将$ data转换为json (ConvertTo-Json $data)

Invoke-WebRequest -Method POST `
-ContentType 'application/json' `
-Body (ConvertTo-Json $data) `
-Uri $url `
-WebSession $websession `
-UseBasicParsing