Exemplo n.º 1
0
        /// <summary>
        /// Create the default credential dialog.
        /// </summary>
        /// <param name="targetName">The target name.</param>
        /// <returns>The credential dialog.</returns>
        public static CredentialDialogOptions Create(string targetName)
        {
            CredentialDialogOptions dialog = new CredentialDialogOptions();

            dialog.AlwaysShowUI         = false;
            dialog.Banner               = null;
            dialog.CompleteUserName     = false;
            dialog.DoNotPersist         = false;
            dialog.ErrorCode            = 0;
            dialog.ExcludeCertificates  = false;
            dialog.ExpectConfirmation   = false;
            dialog.GenericCredentials   = false;
            dialog.IncorrectPassword    = false;
            dialog.Message              = string.Empty;
            dialog.Password             = string.Empty;
            dialog.Persist              = false;
            dialog.RequestAdministrator = false;
            dialog.RequireCertificate   = false;
            dialog.RequireSmartCard     = false;
            dialog.SaveChecked          = false;
            dialog.ShowSaveCheckBox     = false;
            dialog.Title            = string.Empty;
            dialog.UserName         = string.Empty;
            dialog.UserNameReadOnly = false;
            dialog.ValidateUserName = false;
            dialog.TargetName       = targetName;
            return(dialog);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Show the prompt for credentials dialog window.
        /// </summary>
        /// <param name="owner">The windows form prompt dialog owner.</param>
        /// <param name="options">The prompt options.</param>
        /// <returns>The prompt credential result: else null;</returns>
        public CredentialDialogResult ShowPromptForCredentialsDialog(System.Windows.Forms.IWin32Window owner, CredentialDialogOptions options)
        {
            CredentialDialogResult result = null;

            try
            {
                // Create a new credential dialog window.
                using (PromptForCredential prompt = new PromptForCredential())
                {
                    prompt.TargetName = options.TargetName;
                    prompt.UserName   = options.UserName;

                    prompt.Password = new SecureString();
                    foreach (char element in options.Password)
                    {
                        prompt.Password.AppendChar(element);
                    }

                    prompt.Title     = options.Title;
                    prompt.Message   = options.Message;
                    prompt.ErrorCode = options.ErrorCode;

                    if (!String.IsNullOrEmpty(options.Banner))
                    {
                        prompt.Banner = new Bitmap(options.Banner);
                    }

                    prompt.SaveChecked          = options.SaveChecked;
                    prompt.AlwaysShowUI         = options.AlwaysShowUI;
                    prompt.CompleteUserName     = options.CompleteUserName;
                    prompt.DoNotPersist         = options.DoNotPersist;
                    prompt.ExcludeCertificates  = options.ExcludeCertificates;
                    prompt.ExpectConfirmation   = options.ExpectConfirmation;
                    prompt.GenericCredentials   = options.GenericCredentials;
                    prompt.IncorrectPassword    = options.IncorrectPassword;
                    prompt.Persist              = options.Persist;
                    prompt.RequestAdministrator = options.RequestAdministrator;
                    prompt.RequireCertificate   = options.RequireCertificate;
                    prompt.RequireSmartCard     = options.RequireSmartCard;
                    prompt.ShowSaveCheckBox     = options.ShowSaveCheckBox;
                    prompt.UserNameReadOnly     = options.UserNameReadOnly;
                    prompt.ValidateUserName     = options.ValidateUserName;

                    DialogResult dialogResult;
                    if (owner == null)
                    {
                        dialogResult = prompt.ShowDialog();
                    }
                    else
                    {
                        dialogResult = prompt.ShowDialog(owner);
                    }

                    // Show the dialog.
                    if (DialogResult.OK == dialogResult)
                    {
                        // Create the result.
                        result = new CredentialDialogResult();

                        result.SaveChecked = prompt.SaveChecked;
                        result.UserName    = prompt.UserName;

                        IntPtr passwordBstr = IntPtr.Zero;

                        try
                        {
                            // Convert the password pointer data.
                            passwordBstr    = Marshal.SecureStringToBSTR(prompt.Password);
                            result.Password = Marshal.PtrToStringBSTR(passwordBstr);
                        }
                        finally
                        {
                            if (IntPtr.Zero != passwordBstr)
                            {
                                Marshal.FreeBSTR(passwordBstr);
                            }
                        }

                        // Confirm the credentials.
                        if (prompt.ExpectConfirmation && prompt.SaveChecked)
                        {
                            prompt.ConfirmCredentials();
                        }
                    }
                }

                // Return the result.
                return(result);
            }
            catch (Exception)
            {
                throw;
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Show the prompt for credentials dialog window.
 /// </summary>
 /// <param name="options">The prompt options.</param>
 /// <returns>The prompt credential result: else null;</returns>
 public CredentialDialogResult ShowPromptForCredentialsDialog(CredentialDialogOptions options)
 {
     return(ShowPromptForCredentialsDialog(null, options));
 }