Add Support for Null Usernames in Web Cmdlet Basic Auth (#9536)

This commit is contained in:
Mark Kraus 2019-05-06 17:15:04 -07:00 committed by Aditya Patwardhan
parent 2737e74d86
commit 935e164a6d
2 changed files with 29 additions and 1 deletions

View file

@ -805,7 +805,8 @@ namespace Microsoft.PowerShell.Commands
private string GetBasicAuthorizationHeader()
{
string unencoded = string.Format("{0}:{1}", Credential.UserName, Credential.GetNetworkCredential().Password);
var password = new NetworkCredential(null, Credential.Password).Password;
string unencoded = string.Format("{0}:{1}", Credential.UserName, password);
byte[] bytes = Encoding.UTF8.GetBytes(unencoded);
return string.Format("Basic {0}", Convert.ToBase64String(bytes));
}

View file

@ -1455,6 +1455,20 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Headers.Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
}
It "Verifies Invoke-WebRequest -Authentication Basic with null username" {
$credential = [pscredential]::new([PSCustomObject]@{UserName = $null;Password=$token.psobject.BaseObject})
$params = @{
Uri = $httpsUri
Authentication = "Basic"
Credential = $credential
SkipCertificateCheck = $true
}
$Response = Invoke-WebRequest @params
$result = $response.Content | ConvertFrom-Json
$result.Headers.Authorization | Should -BeExactly "Basic OnRlc3RwYXNzd29yZA=="
}
It "Verifies Invoke-WebRequest -Authentication <Authentication>" -TestCases $testCases {
param($Authentication)
$params = @{
@ -2779,6 +2793,19 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$result.Headers.Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
}
It "Verifies Invoke-RestMethod -Authentication Basic with null username" {
$credential = [pscredential]::new([PSCustomObject]@{UserName = $null;Password=$token.psobject.BaseObject})
$params = @{
Uri = $httpsUri
Authentication = "Basic"
Credential = $credential
SkipCertificateCheck = $true
}
$Response = Invoke-RestMethod @params
$Response.Headers.Authorization | Should -BeExactly "Basic OnRlc3RwYXNzd29yZA=="
}
It "Verifies Invoke-RestMethod -Authentication <Authentication>" -TestCases $testCases {
param($Authentication)
$params = @{