Adding a cookie to a System.Net.CookieContainer object

Welcome Forums General PowerShell Q&A Adding a cookie to a System.Net.CookieContainer object

This topic contains 4 replies, has 2 voices, and was last updated by

 
Participant
3 years, 7 months ago.

  • Author
    Posts
  • #26104

    Participant
    Points: 1
    Rank: Member

    Hi all, I have been breaking my head over the following:

    I have a ProxMox server with a JSON interface and want to use Powershell to run some commands on it.

    With Curl on Linux it is easy:
    1) curl -k -d "username=username@pam&password=password" https://10.0.0.1:8006/api2/json/access/ticket
    2) This returns some JSON formatted stuff with a 'ticket' and a 'CSRFPreventionToken'.
    3) In subsequent requests, pass the ticket or the CSRFPreventionToken as a cookie:
    curl -k -b "PVEAuthCookie=PVE:username@pam:4EEC61E2:..." https://10.0.0.1:8006/api2/json/

    With Invoke-Restmethod, I was able to get the ticket. However, since the server does not actually set it as a cookie, the -Sessionvariable parameter leaves me with an empty cookiejar.

    $uri='https://proxmox:8006/api2/json/access/ticket'
    $body =@{
    username='username'
    password='password'
    }
    $ticket = Invoke-RestMethod -Method Post -Uri $uri -Body $body -SessionVariable proxmox

    Gives me 2 variables:
    $ticket.data -> Holds the tokens (ticket and CSRFPreventionToken)
    $proxmox.Cookies -> System.Net.CookieContainer with 0 cookies in it.

    I cannot find any useful information on the System.Net.CookieContainer, nor on the properties 'Add' or 'SetCookies'..

    How can I add a the two tokens from $ticket.data to my $proxmox.Cookies jar?

  • #26105

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    So... I mean, this is really kind of a .NET question, so it's possible a site like StackOverflow will get you a more immediate answer. And, I know zip about ProxMox.

    According to https://msdn.microsoft.com/en-us/library/system.net.cookiecontainer(v=vs.110).aspx, though, you have to construct a Cookie object, and then call the Add() method of the CookieContainer to add it.

  • #26110

    Participant
    Points: 1
    Rank: Member

    Thanks for the response, Don. I figured that I would look at some .Net stuff, but since I am not a developer, it is not really helpful Some if the MSDN documentation I found (and tried) included constructing cookies like:

    $proxmox.Cookies.Add(System.Net.Cookie "name")
    $proxmox.Cookies.SetCookies(uri $uri)
    $proxmox.Cookies.SetCookies(ticket='test')

    etc.. But all to no avail. If I would just understand the construct of Add() or SetCookies(), I might get somewhere...

  • #26140

    Participant
    Points: 1
    Rank: Member

    Quick update on my own thread. I found how to put the cookie in place:

    function Create-Cookie($name, $value, $domain, $path="/"){
    $c=New-Object System.Net.Cookie;
    $c.Name=$name;
    $c.Path=$path;
    $c.Value = $value
    $c.Domain =$domain;
    return $c;
    }

    $uri='https://proxmox:8006/api2/json'
    $body =@{
    username='user@pve'
    password='password'
    }
    $ticketuri = $uri+'/access/ticket'
    $ticket = Invoke-RestMethod -Method Post -Uri $ticketuri -Body $body -SessionVariable proxmox

    #Create the cookies for the session based on the data returned
    $PVEAuthCookie=$ticket.data.ticket

    $proxmox.Cookies.Add((Create-Cookie -name "PVEAuthCookie" -value "$PVEAuthCookie" -domain "proxmox"));

    Invoke-RestMethod $uri -SessionVariable $proxmox

    (I know my code is quite verbose and is not as clean as it could be. Makes it easier to troubleshoot)

    The cookie is passed and the Proxmox log shows:
    Jun 8 18:10:43 PROXMOX pvedaemon[730732]: successful auth for user 'user@pve'

    Oddly enough, Powershell bleeds with:
    Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.

    If I figure it out, I will post here... In the meantime, any ideas are welcome.

  • #26758

    Participant
    Points: 1
    Rank: Member

    OK – I found the issue. Amazing that nobody noticed this one:

    Invoke-RestMethod $uri -SessionVariable $proxmox

    Should be:
    Invoke-RestMethod $uri -WebSession $proxmox

    Works fine now.. 😀

    I hope this is useful to somebody at some point...

The topic ‘Adding a cookie to a System.Net.CookieContainer object’ is closed to new replies.