From d9da234cdf854f547adb1092a738e5f6c65e7fb9 Mon Sep 17 00:00:00 2001 From: Steve Lee Date: Mon, 8 Mar 2021 10:52:02 -0800 Subject: [PATCH] Fix `PromptForCredential()` to add `targetName` as domain (#14504) --- .../msh/ConsoleHostUserInterfaceSecurity.cs | 25 +++++++++++++------ .../engine/Utils.cs | 1 + test/powershell/Host/HostUtilities.Tests.ps1 | 20 +++++++++++++++ 3 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs index 67b4fc6a5..1e934553f 100644 --- a/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs +++ b/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs @@ -97,17 +97,26 @@ namespace Microsoft.PowerShell passwordPrompt = StringUtil.Format(ConsoleHostUserInterfaceSecurityResources.PromptForCredential_Password, userName ); - // - // now, prompt for the password - // - WriteToConsole(passwordPrompt, true); - password = ReadLineAsSecureString(); - if (password == null) + if (!InternalTestHooks.NoPromptForPassword) { - return null; + WriteToConsole(passwordPrompt, transcribeResult: true); + password = ReadLineAsSecureString(); + if (password == null) + { + return null; + } + + WriteLineToConsole(); + } + else + { + password = new SecureString(); } - WriteLineToConsole(); + if (!string.IsNullOrEmpty(targetName)) + { + userName = StringUtil.Format("{0}\\{1}", targetName, userName); + } cred = new PSCredential(userName, password); diff --git a/src/System.Management.Automation/engine/Utils.cs b/src/System.Management.Automation/engine/Utils.cs index fa1ef1357..94957ced8 100644 --- a/src/System.Management.Automation/engine/Utils.cs +++ b/src/System.Management.Automation/engine/Utils.cs @@ -2072,6 +2072,7 @@ namespace System.Management.Automation.Internal internal static bool BypassOnlineHelpRetrieval; internal static bool ForcePromptForChoiceDefaultOption; internal static bool BypassOutputRedirectionCheck; + internal static bool NoPromptForPassword; // Stop/Restart/Rename Computer tests internal static bool TestStopComputer; diff --git a/test/powershell/Host/HostUtilities.Tests.ps1 b/test/powershell/Host/HostUtilities.Tests.ps1 index 6c791b5c4..a88689108 100644 --- a/test/powershell/Host/HostUtilities.Tests.ps1 +++ b/test/powershell/Host/HostUtilities.Tests.ps1 @@ -58,3 +58,23 @@ Describe "InvokeOnRunspace method on remote runspace" -tags "Feature","RequireAd $results[0] | Should -Be "Hello!" } } + +Describe 'PromptForCredential' { + BeforeAll { + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $true) + } + + AfterAll { + [System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $false) + } + + It 'Should accept no targetname' { + $out = $Host.UI.PromptForCredential('caption','message','myUser',$null) + $out.UserName | Should -BeExactly 'myUser' + } + + It 'Should accept targetname as domain' { + $out = $Host.UI.PromptForCredential('caption','message','myUser','myDomain') + $out.UserName | Should -BeExactly 'myDomain\myUser' + } +}