Add Support for Null Usernames in Web Cmdlet Basic Auth (#9536)
This commit is contained in:
parent
2737e74d86
commit
935e164a6d
|
@ -805,7 +805,8 @@ namespace Microsoft.PowerShell.Commands
|
||||||
|
|
||||||
private string GetBasicAuthorizationHeader()
|
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);
|
byte[] bytes = Encoding.UTF8.GetBytes(unencoded);
|
||||||
return string.Format("Basic {0}", Convert.ToBase64String(bytes));
|
return string.Format("Basic {0}", Convert.ToBase64String(bytes));
|
||||||
}
|
}
|
||||||
|
|
|
@ -1455,6 +1455,20 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
|
||||||
$result.Headers.Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
|
$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 {
|
It "Verifies Invoke-WebRequest -Authentication <Authentication>" -TestCases $testCases {
|
||||||
param($Authentication)
|
param($Authentication)
|
||||||
$params = @{
|
$params = @{
|
||||||
|
@ -2779,6 +2793,19 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
|
||||||
$result.Headers.Authorization | Should -BeExactly "Basic dGVzdHVzZXI6dGVzdHBhc3N3b3Jk"
|
$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 {
|
It "Verifies Invoke-RestMethod -Authentication <Authentication>" -TestCases $testCases {
|
||||||
param($Authentication)
|
param($Authentication)
|
||||||
$params = @{
|
$params = @{
|
||||||
|
|
Loading…
Reference in a new issue