Skip to content

Commit d9da234

Browse files
authored
Fix PromptForCredential() to add targetName as domain (#14504)
1 parent ae5a84e commit d9da234

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterfaceSecurity.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,26 @@ public override PSCredential PromptForCredential(
9797
passwordPrompt = StringUtil.Format(ConsoleHostUserInterfaceSecurityResources.PromptForCredential_Password, userName
9898
);
9999

100-
//
101-
// now, prompt for the password
102-
//
103-
WriteToConsole(passwordPrompt, true);
104-
password = ReadLineAsSecureString();
105-
if (password == null)
100+
if (!InternalTestHooks.NoPromptForPassword)
106101
{
107-
return null;
102+
WriteToConsole(passwordPrompt, transcribeResult: true);
103+
password = ReadLineAsSecureString();
104+
if (password == null)
105+
{
106+
return null;
107+
}
108+
109+
WriteLineToConsole();
110+
}
111+
else
112+
{
113+
password = new SecureString();
108114
}
109115

110-
WriteLineToConsole();
116+
if (!string.IsNullOrEmpty(targetName))
117+
{
118+
userName = StringUtil.Format("{0}\\{1}", targetName, userName);
119+
}
111120

112121
cred = new PSCredential(userName, password);
113122

src/System.Management.Automation/engine/Utils.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2072,6 +2072,7 @@ public static class InternalTestHooks
20722072
internal static bool BypassOnlineHelpRetrieval;
20732073
internal static bool ForcePromptForChoiceDefaultOption;
20742074
internal static bool BypassOutputRedirectionCheck;
2075+
internal static bool NoPromptForPassword;
20752076

20762077
// Stop/Restart/Rename Computer tests
20772078
internal static bool TestStopComputer;

test/powershell/Host/HostUtilities.Tests.ps1

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,23 @@ Describe "InvokeOnRunspace method on remote runspace" -tags "Feature","RequireAd
5858
$results[0] | Should -Be "Hello!"
5959
}
6060
}
61+
62+
Describe 'PromptForCredential' {
63+
BeforeAll {
64+
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $true)
65+
}
66+
67+
AfterAll {
68+
[System.Management.Automation.Internal.InternalTestHooks]::SetTestHook('NoPromptForPassword', $false)
69+
}
70+
71+
It 'Should accept no targetname' {
72+
$out = $Host.UI.PromptForCredential('caption','message','myUser',$null)
73+
$out.UserName | Should -BeExactly 'myUser'
74+
}
75+
76+
It 'Should accept targetname as domain' {
77+
$out = $Host.UI.PromptForCredential('caption','message','myUser','myDomain')
78+
$out.UserName | Should -BeExactly 'myDomain\myUser'
79+
}
80+
}

0 commit comments

Comments
 (0)