/// <summary>
        /// Prompt for credentials
        /// </summary>
        /// <param name="target">Target to the credentials</param>
        /// <param name="username">receives user name</param>
        /// <param name="password">receives password associated</param>
        /// <param name="fSave">receives if credentials are saved.</param>
        /// <param name="fConsole">Is this a console application</param>
        /// <param name="hwndParent">Parent window handle</param>
        /// <param name="flags">options for the window.</param>
        /// <param name="message">Message to end user included in the window.</param>
        internal static void PromptForCredentials(string target, ref string username, ref SecureString password, ref bool fSave, bool fConsole, IntPtr hwndParent, CredentialNativeMethods.CREDUI_FLAGS flags, string message)
        {
            CredentialNativeMethods.CREDUI_INFO info = new CredentialNativeMethods.CREDUI_INFO();
            int result;

            int           saveCredentials;
            StringBuilder user = new StringBuilder(username, CRED_MAX_USERNAME_LENGTH);
            StringBuilder pwd  = new StringBuilder(CRED_MAX_PASSWORD_LENGTH);

            saveCredentials = 0;
            info.cbSize     = Marshal.SizeOf(info);
            info.hwndParent = hwndParent;

            if (!string.IsNullOrEmpty(message))
            {
                // Use default message text, when message is not specified.
                info.pszMessageText = message;
            }

            if (fConsole)
            {
                result = CredentialNativeMethods.CredUICmdLinePromptForCredentials(target,
                                                                                   IntPtr.Zero,
                                                                                   0,
                                                                                   user,
                                                                                   CRED_MAX_USERNAME_LENGTH,
                                                                                   pwd,
                                                                                   CRED_MAX_PASSWORD_LENGTH,
                                                                                   ref saveCredentials,
                                                                                   flags);
            }
            else
            {
                if (hwndParent == new IntPtr(-1))
                {
                    throw new System.Security.Authentication.InvalidCredentialException();
                }
                result = CredentialNativeMethods.CredUIPromptForCredentials(ref info,
                                                                            target,
                                                                            IntPtr.Zero,
                                                                            0,
                                                                            user,
                                                                            CRED_MAX_USERNAME_LENGTH,
                                                                            pwd,
                                                                            CRED_MAX_PASSWORD_LENGTH,
                                                                            ref saveCredentials,
                                                                            flags);
            }
            if (result != 0)
            {
                throw new System.ComponentModel.Win32Exception(result);
            }

            fSave    = (saveCredentials != 0);
            username = user.ToString();
            password = new SecureString();
            for (int i = 0; i < pwd.Length; i++)
            {
                password.AppendChar(pwd[i]);
                pwd[i] = ' ';                   // overwrite the cleartext password
            }
            password.MakeReadOnly();
        }
        /// <summary>
        /// Prompt for credentials
        /// </summary>
        /// <param name="target">Target to the credentials</param>
        /// <param name="username">receives user name</param>
        /// <param name="password">receives password associated</param>
        /// <param name="fSave">receives if credentials are saved.</param>
        /// <param name="fConsole">Is this a console application</param>
        /// <param name="hwndParent">Parent window handle</param>
        /// <param name="flags">options for the window.</param>
        /// <param name="message">Message to end user included in the window.</param>
        internal static void PromptForCredentials(string target, ref string username, ref SecureString password, ref bool fSave, bool fConsole, IntPtr hwndParent, CredentialNativeMethods.CREDUI_FLAGS flags, string message)
        {
            CredentialNativeMethods.CREDUI_INFO info = new CredentialNativeMethods.CREDUI_INFO();
            int result;

            int saveCredentials;
            StringBuilder user = new StringBuilder(username, CRED_MAX_USERNAME_LENGTH);
            StringBuilder pwd = new StringBuilder(CRED_MAX_PASSWORD_LENGTH);
            saveCredentials = 0;
            info.cbSize = Marshal.SizeOf(info);
            info.hwndParent = hwndParent;

            if (!string.IsNullOrEmpty(message))
            {
                // Use default message text, when message is not specified.
                info.pszMessageText = message;
            }

            if (fConsole)
            {
                result = CredentialNativeMethods.CredUICmdLinePromptForCredentials(target,
                                                           IntPtr.Zero,
                                                           0,
                                                           user,
                                                           CRED_MAX_USERNAME_LENGTH,
                                                           pwd,
                                                           CRED_MAX_PASSWORD_LENGTH,
                                                           ref saveCredentials,
                                                           flags);
            }
            else
            {
                if (hwndParent == new IntPtr(-1))
                {
                    throw new System.Security.Authentication.InvalidCredentialException();
                }
                result = CredentialNativeMethods.CredUIPromptForCredentials(ref info,
                                                    target,
                                                    IntPtr.Zero,
                                                    0,
                                                    user,
                                                    CRED_MAX_USERNAME_LENGTH,
                                                    pwd,
                                                    CRED_MAX_PASSWORD_LENGTH,
                                                    ref saveCredentials,
                                                    flags);
            }
            if (result != 0)
            {
                throw new System.ComponentModel.Win32Exception(result);
            }

            fSave = (saveCredentials != 0);
            username = user.ToString();
            password = new SecureString();
            for (int i = 0; i < pwd.Length; i++)
            {
                password.AppendChar(pwd[i]);
                pwd[i] = ' ';                   // overwrite the cleartext password
            }
            password.MakeReadOnly();
        }