private static extern HostUtilities.CredUIReturnCodes CredUIPromptForCredentials( ref HostUtilities.CREDUI_INFO pUiInfo, string pszTargetName, IntPtr Reserved, int dwAuthError, StringBuilder pszUserName, int ulUserNameMaxChars, StringBuilder pszPassword, int ulPasswordMaxChars, ref int pfSave, HostUtilities.CREDUI_FLAGS dwFlags);
internal static PSCredential CredUIPromptForCredential( string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options, IntPtr parentHWND) { using (HostUtilities.tracer.TraceMethod()) { if (string.IsNullOrEmpty(caption)) { caption = ResourceManagerCache.GetResourceString("CredUI", "PromptForCredential_DefaultCaption"); } if (string.IsNullOrEmpty(message)) { message = ResourceManagerCache.GetResourceString("CredUI", "PromptForCredential_DefaultMessage"); } HostUtilities.CREDUI_INFO pUiInfo = new HostUtilities.CREDUI_INFO(); pUiInfo.pszCaptionText = caption; pUiInfo.pszMessageText = message; StringBuilder pszUserName = new StringBuilder(userName, 513); StringBuilder pszPassword = new StringBuilder(256); int int32 = Convert.ToInt32(false); pUiInfo.cbSize = Marshal.SizeOf((object)pUiInfo); pUiInfo.hwndParent = parentHWND; HostUtilities.CREDUI_FLAGS dwFlags = HostUtilities.CREDUI_FLAGS.DO_NOT_PERSIST; if ((allowedCredentialTypes & PSCredentialTypes.Domain) != PSCredentialTypes.Domain) { dwFlags |= HostUtilities.CREDUI_FLAGS.GENERIC_CREDENTIALS; if ((options & PSCredentialUIOptions.AlwaysPrompt) == PSCredentialUIOptions.AlwaysPrompt) { dwFlags |= HostUtilities.CREDUI_FLAGS.ALWAYS_SHOW_UI; } } HostUtilities.CredUIReturnCodes credUiReturnCodes = HostUtilities.CredUIReturnCodes.ERROR_INVALID_PARAMETER; if (pszUserName.Length <= 513 && pszPassword.Length <= 256) { credUiReturnCodes = HostUtilities.CredUIPromptForCredentials(ref pUiInfo, targetName, IntPtr.Zero, 0, pszUserName, 513, pszPassword, 256, ref int32, dwFlags); } PSCredential psCredential; switch (credUiReturnCodes) { case HostUtilities.CredUIReturnCodes.NO_ERROR: string userName1 = (string)null; if (pszUserName != null) { userName1 = pszUserName.ToString(); } SecureString password = new SecureString(); for (int index = 0; index < pszPassword.Length; ++index) { password.AppendChar(pszPassword[index]); pszPassword[index] = char.MinValue; } psCredential = string.IsNullOrEmpty(userName1) ? (PSCredential)null : new PSCredential(userName1, password); break; case HostUtilities.CredUIReturnCodes.ERROR_CANCELLED: psCredential = (PSCredential)null; break; default: HostUtilities.tracer.TraceError("CredUIPromptForCredentials returned an error: " + credUiReturnCodes.ToString()); goto case HostUtilities.CredUIReturnCodes.ERROR_CANCELLED; } return(psCredential); } }