/// <summary>
        /// When overridden in a derived class, specifies a common dialog box.
        /// </summary>
        /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param>
        /// <returns>
        /// true if the dialog box was successfully run; otherwise, false.
        /// </returns>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            if (Environment.OSVersion.Version.Major < 5)
            {
                throw new PlatformNotSupportedException(Properties.Resources.FunctionNotSupported);
            }

            UnsafeNativeMethods.CredUIInfo credInfo = new UnsafeNativeMethods.CredUIInfo(hwndOwner,
                                                                                         this.caption, this.message, this.banner);
            StringBuilder usr = new StringBuilder(UnsafeNativeMethods.CREDUI_MAX_USERNAME_LENGTH);
            StringBuilder pwd = new StringBuilder(UnsafeNativeMethods.CREDUI_MAX_PASSWORD_LENGTH);

            if (this.credentials != null)
            {
                usr.Append(this.credentials.PrincipalName);
                pwd.Append(this.credentials.PasswordToString());
            }

            try
            {
                //For more info see:
                //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/creduipromptforcredentials.asp
                //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/dpapiusercredentials.asp?frame=true

                UnsafeNativeMethods.CredUIReturnCodes result = UnsafeNativeMethods.CredUIPromptForCredentials(
                    ref credInfo, this.target,
                    IntPtr.Zero, 0,
                    usr, UnsafeNativeMethods.CREDUI_MAX_USERNAME_LENGTH,
                    pwd, UnsafeNativeMethods.CREDUI_MAX_PASSWORD_LENGTH,
                    ref this.saveChecked, this.flags);
                switch (result)
                {
                case UnsafeNativeMethods.CredUIReturnCodes.NO_ERROR:
                    this.Credentials = new SecureCredential(usr.ToString(), pwd);
                    return(true);

                case UnsafeNativeMethods.CredUIReturnCodes.ERROR_CANCELLED:
                    this.Credentials = null;
                    return(false);

                default:
                    throw new InvalidOperationException(TranslateReturnCode(result));
                }
            }
            finally
            {
                usr.Remove(0, usr.Length);
                pwd.Remove(0, pwd.Length);
                if (this.banner != null)
                {
                    UnsafeNativeMethods.DeleteObject(credInfo.hbmBanner);
                }
            }
        }
        /// <summary>
        /// When overridden in a derived class, specifies a common dialog box.
        /// </summary>
        /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param>
        /// <returns>
        /// true if the dialog box was successfully run; otherwise, false.
        /// </returns>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            if (Environment.OSVersion.Version.Major < 5)
            {
                throw new PlatformNotSupportedException(Properties.Resources.FunctionNotSupported);
            }

            UnsafeNativeMethods.CredUIInfo credInfo = new UnsafeNativeMethods.CredUIInfo(hwndOwner,
                this.caption, this.message, this.banner);
            StringBuilder usr = new StringBuilder(UnsafeNativeMethods.CREDUI_MAX_USERNAME_LENGTH);
            StringBuilder pwd = new StringBuilder(UnsafeNativeMethods.CREDUI_MAX_PASSWORD_LENGTH);

            if (this.credentials != null)
            {
                usr.Append(this.credentials.PrincipalName);
                pwd.Append(this.credentials.PasswordToString());
            }

            try
            {
                //For more info see:
                //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secauthn/security/creduipromptforcredentials.asp
                //http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/dpapiusercredentials.asp?frame=true

                UnsafeNativeMethods.CredUIReturnCodes result = UnsafeNativeMethods.CredUIPromptForCredentials(
                                                        ref credInfo, this.target,
                                                        IntPtr.Zero, 0,
                                                        usr, UnsafeNativeMethods.CREDUI_MAX_USERNAME_LENGTH,
                                                        pwd, UnsafeNativeMethods.CREDUI_MAX_PASSWORD_LENGTH,
                                                        ref this.saveChecked, this.flags);
                switch (result)
                {
                    case UnsafeNativeMethods.CredUIReturnCodes.NO_ERROR:
                        this.Credentials = new SecureCredential(usr.ToString(), pwd);
                        return true;
                    case UnsafeNativeMethods.CredUIReturnCodes.ERROR_CANCELLED:
                        this.Credentials = null;
                        return false;
                    default:
                        throw new InvalidOperationException(TranslateReturnCode(result));
                }
            }
            finally
            {
                usr.Remove(0, usr.Length);
                pwd.Remove(0, pwd.Length);
                if (this.banner != null)
                {
                    UnsafeNativeMethods.DeleteObject(credInfo.hbmBanner);
                }
            }
        }