@@ -78,127 +78,61 @@ public override PSCredential PromptForCredential(
7878 PSCredentialTypes allowedCredentialTypes ,
7979 PSCredentialUIOptions options )
8080 {
81- if ( ! PromptUsingConsole ( ) )
82- {
83- IntPtr mainWindowHandle = GetMainWindowHandle ( ) ;
84- return HostUtilities . CredUIPromptForCredential ( caption , message , userName , targetName , allowedCredentialTypes , options , mainWindowHandle ) ;
85- }
86- else
87- {
88- PSCredential cred = null ;
89- SecureString password = null ;
90- string userPrompt = null ;
91- string passwordPrompt = null ;
92-
93- if ( ! string . IsNullOrEmpty ( caption ) )
94- {
95- // Should be a skin lookup
81+ PSCredential cred = null ;
82+ SecureString password = null ;
83+ string userPrompt = null ;
84+ string passwordPrompt = null ;
9685
97- WriteLineToConsole ( ) ;
98- WriteToConsole ( PromptColor , RawUI . BackgroundColor , WrapToCurrentWindowWidth ( caption ) ) ;
99- WriteLineToConsole ( ) ;
100- }
101-
102- if ( ! string . IsNullOrEmpty ( message ) )
103- {
104- WriteLineToConsole ( WrapToCurrentWindowWidth ( message ) ) ;
105- }
86+ if ( ! string . IsNullOrEmpty ( caption ) )
87+ {
88+ // Should be a skin lookup
10689
107- if ( string . IsNullOrEmpty ( userName ) )
108- {
109- userPrompt = ConsoleHostUserInterfaceSecurityResources . PromptForCredential_User ;
90+ WriteLineToConsole ( ) ;
91+ WriteToConsole ( PromptColor , RawUI . BackgroundColor , WrapToCurrentWindowWidth ( caption ) ) ;
92+ WriteLineToConsole ( ) ;
93+ }
11094
111- //
112- // need to prompt for user name first
113- //
114- do
115- {
116- WriteToConsole ( userPrompt , true ) ;
117- userName = ReadLine ( ) ;
118- if ( userName == null )
119- {
120- return null ;
121- }
122- }
123- while ( userName . Length == 0 ) ;
124- }
95+ if ( ! string . IsNullOrEmpty ( message ) )
96+ {
97+ WriteLineToConsole ( WrapToCurrentWindowWidth ( message ) ) ;
98+ }
12599
126- passwordPrompt = StringUtil . Format ( ConsoleHostUserInterfaceSecurityResources . PromptForCredential_Password , userName
127- ) ;
100+ if ( string . IsNullOrEmpty ( userName ) )
101+ {
102+ userPrompt = ConsoleHostUserInterfaceSecurityResources . PromptForCredential_User ;
128103
129104 //
130- // now, prompt for the password
105+ // need to prompt for user name first
131106 //
132- WriteToConsole ( passwordPrompt , true ) ;
133- password = ReadLineAsSecureString ( ) ;
134- if ( password == null )
107+ do
135108 {
136- return null ;
109+ WriteToConsole ( userPrompt , true ) ;
110+ userName = ReadLine ( ) ;
111+ if ( userName == null )
112+ {
113+ return null ;
114+ }
137115 }
138- WriteLineToConsole ( ) ;
139-
140- cred = new PSCredential ( userName , password ) ;
141-
142- return cred ;
116+ while ( userName . Length == 0 ) ;
143117 }
144- }
145118
146- private IntPtr GetMainWindowHandle ( )
147- {
148- #if CORECLR // No System.Diagnostics.Process.MainWindowHandle on CoreCLR;
149- // Returned WindowHandle is used only in 1 case - prompting for credential using GUI dialog, which is not used on Nano,
150- // because on Nano we prompt for credential using console (different code path in 'PromptForCredential' function)
151- return IntPtr . Zero ;
152- #else
153- System . Diagnostics . Process currentProcess = System . Diagnostics . Process . GetCurrentProcess ( ) ;
154- IntPtr mainWindowHandle = currentProcess . MainWindowHandle ;
119+ passwordPrompt = StringUtil . Format ( ConsoleHostUserInterfaceSecurityResources . PromptForCredential_Password , userName
120+ ) ;
155121
156- while ( ( mainWindowHandle == IntPtr . Zero ) && ( currentProcess != null ) )
122+ //
123+ // now, prompt for the password
124+ //
125+ WriteToConsole ( passwordPrompt , true ) ;
126+ password = ReadLineAsSecureString ( ) ;
127+ if ( password == null )
157128 {
158- currentProcess = PsUtils . GetParentProcess ( currentProcess ) ;
159- if ( currentProcess != null )
160- {
161- mainWindowHandle = currentProcess . MainWindowHandle ;
162- }
129+ return null ;
163130 }
131+ WriteLineToConsole ( ) ;
164132
165- return mainWindowHandle ;
166- #endif
167- }
168-
169- // Determines whether we should prompt using the Console prompting
170- // APIs
171- private bool PromptUsingConsole ( )
172- {
173- #if CORECLR
174- // on Nano there is no other way to prompt except by using console
175- return true ;
176- #else
177- bool promptUsingConsole = false ;
178- // Get the configuration setting
179- try
180- {
181- promptUsingConsole = ConfigPropertyAccessor . Instance . GetConsolePrompting ( ) ;
182- }
183- catch ( System . Security . SecurityException e )
184- {
185- s_tracer . TraceError ( "Could not read CredUI registry key: " + e . Message ) ;
186- return promptUsingConsole ;
187- }
188- catch ( InvalidCastException e )
189- {
190- s_tracer . TraceError ( "Could not parse CredUI registry key: " + e . Message ) ;
191- return promptUsingConsole ;
192- }
193- catch ( FormatException e )
194- {
195- s_tracer . TraceError ( "Could not parse CredUI registry key: " + e . Message ) ;
196- return promptUsingConsole ;
197- }
133+ cred = new PSCredential ( userName , password ) ;
198134
199- s_tracer . WriteLine ( "DetermineCredUIPolicy: policy == {0}" , promptUsingConsole ) ;
200- return promptUsingConsole ;
201- #endif
135+ return cred ;
202136 }
203137 }
204138}
0 commit comments