diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs index 1d06490509..d082020bb2 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs @@ -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)); } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 index 9f69785cd1..3047325283 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1 @@ -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 " -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 " -TestCases $testCases { param($Authentication) $params = @{