Fix PromptForCredential() to add targetName as domain (#14504)

This commit is contained in:
Steve Lee 2021-03-08 10:52:02 -08:00 committed by Aditya Patwardhan
parent 82737fab56
commit 98b83161c3
3 changed files with 39 additions and 8 deletions

View file

@ -101,17 +101,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);

View file

@ -2056,6 +2056,8 @@ namespace System.Management.Automation.Internal
internal static bool BypassAppLockerPolicyCaching;
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;

View file

@ -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'
}
}