internal static PSCredential CredUIPromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options, IntPtr parentHWND) { PSCredential credential = null; CreduiInfo structure = new CreduiInfo(); structure.pszCaptionText = caption; structure.pszMessageText = message; StringBuilder pszUserName = new StringBuilder(userName, 0x201); StringBuilder pszPassword = new StringBuilder(0x100); bool flag = false; int pfSave = Convert.ToInt32(flag); structure.cbSize = Marshal.SizeOf(structure); structure.hwndParent = parentHWND; CreduiFlags dwFlags = CreduiFlags.DO_NOT_PERSIST; if ((allowedCredentialTypes & PSCredentialTypes.Domain) != PSCredentialTypes.Domain) { dwFlags |= CreduiFlags.GENERIC_CREDENTIALS; if ((options & PSCredentialUIOptions.AlwaysPrompt) == PSCredentialUIOptions.AlwaysPrompt) { dwFlags |= CreduiFlags.ALWAYS_SHOW_UI; } } CredUiReturnCodes codes = CredUiReturnCodes.ERROR_INVALID_PARAMETER; if ((pszUserName.Length <= 0x201) && (pszPassword.Length <= 0x100)) { codes = CredUIPromptForCredentials(ref structure, targetName, IntPtr.Zero, 0, pszUserName, 0x201, pszPassword, 0x100, ref pfSave, dwFlags); } if (codes == CredUiReturnCodes.NO_ERROR) { string str = null; if (pszUserName != null) { str = pszUserName.ToString(); } SecureString password = new SecureString(); for (int i = 0; i < pszPassword.Length; i++) { password.AppendChar(pszPassword[i]); pszPassword[i] = '\0'; } if (!string.IsNullOrEmpty(str)) { credential = new PSCredential(str, password); } else { credential = null; } } else { credential = null; } return(credential); }
private static extern CredUiReturnCodes CredUIPromptForCredentials(ref CreduiInfo pUiInfo, string pszTargetName, IntPtr Reserved, int dwAuthError, StringBuilder pszUserName, int ulUserNameMaxChars, StringBuilder pszPassword, int ulPasswordMaxChars, ref int pfSave, CreduiFlags dwFlags);