Add password confirmation in Get-Credential (#12782)

* Add parameter -ReEnterPassword to prompt user to re-enter the password for confirmation

* Add new public overload function PromptForCredential
This commit is contained in:
Shri Ram K S
2020-07-22 19:13:42 +05:00
committed by GitHub
parent 9c396aa0ea
commit a6bd47f031
11 changed files with 329 additions and 79 deletions
@@ -102,4 +102,21 @@ Describe "Get-Credential Test" -Tag "CI" {
$netcred.UserName | Should -Be "John"
$netcred.Password | Should -Be "CredTest"
}
It "Get-credential Joe -ConfirmPassword set to false"{
$cred = $ps.AddScript("Get-Credential Joe -ConfirmPassword:`$false").Invoke() | Select-Object -First 1
$cred | Should -BeOfType System.Management.Automation.PSCredential
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should -Be "Joe"
$netcred.Password | Should -Be "This is a test"
$th.ui.Streams.Prompt[-1] | Should -Match "Credential:[^:]+:[^:]+"
}
It "Get-Credential with only -ConfirmPassword" {
$cred = $ps.AddScript("Get-Credential -ConfirmPassword").Invoke() | Select-Object -First 1
$cred | Should -BeOfType System.Management.Automation.PSCredential
$netcred = $cred.GetNetworkCredential()
$netcred.UserName | Should -Be "John"
$netcred.Password | Should -Be "This is a test"
$th.ui.Streams.Prompt[-2] | Should -Match "Credential:[^:]+:[^:]+"
$th.ui.Streams.Prompt[-1] | Should -Match "Credential@[^@]+@[^@]+"
}
}
@@ -10,6 +10,7 @@ using System.Globalization;
using System.Collections.ObjectModel;
using System.Security;
using System.Collections;
using System.Runtime.InteropServices;
namespace TestHost
{
@@ -145,18 +146,36 @@ namespace TestHost
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
Streams.Prompt.Add("Credential:" + caption + ":" + message);
SecureString ss = ReadLineAsSecureString();
string userNameToUse = string.IsNullOrEmpty(userName) ? UserNameForCredential : userName;
return new PSCredential(userNameToUse, ss);
return PromptForCredential(caption,
message,
userName,
confirmPassword: false,
targetName,
PSCredentialTypes.Default,
PSCredentialUIOptions.Default);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
return PromptForCredential(caption,
message,
userName,
confirmPassword: false,
targetName,
allowedCredentialTypes,
options);
}
public override PSCredential PromptForCredential(string caption, string message, string userName, bool confirmPassword, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
Streams.Prompt.Add("Credential:" + caption + ":" + message);
SecureString ss = ReadLineAsSecureString();
SecureString password = ReadLineAsSecureString();
if(confirmPassword)
{
Streams.Prompt.Add("Credential@" + caption + "@" + message);
}
string userNameToUse = string.IsNullOrEmpty(userName) ? UserNameForCredential : userName;
return new PSCredential(userNameToUse, ss);
return new PSCredential(userNameToUse, password);
}
public override string ReadLine()