Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PowerShell not send Authentication headers for non-default ports?

Tags:

powershell

Something odd I noticed... in Powershell, if I attempt the following:

$uri = new-object System.Uri("http://builds.strongbadia.egg:8080/builds/package.tar.bz2")
$clnt = new-object System.Net.WebClient
$clnt.Credentials = new-object System.Net.NetworkCredential($username, $password)
$clnt.DownloadFile($uri, "package.tar.bz2")

The "Authorization" header is not sent (confirmed via Wireshark). Even if I use the credential cache and specifically state Basic auth should be used:

$cred = new-object System.Net.NetworkCredential($username, $password)
$credcache = new-object System.Net.CredentialCache
$credcache.Add($uri, "Basic", $cred)
$clnt.Credentials = $credcache

It doesn't send the header at all. Instead I must do the following:

$auth = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+":"+$password ))
$clnt.Headers.Add('Authorization', $auth)

Only then does the "Authorization" header get sent, and that was basically because I was ramming things down PowerShell's throat.

Any idea if this is a known defect or something I've done wrong? I can't seem to find reference to it.

like image 965
DeckerEgo Avatar asked Apr 21 '26 21:04

DeckerEgo


1 Answers

I have actually seen this before, basically the .Net libraries wait for a challenege for authentication from the server before sending the credentials. The problem is that some servers will never send the request and that's why you don't ever see your header being added. The reason your way works is that you are explicitly forcing the header onto the headers collection.

Also its not just PowerShell it happens in any .Net language.