コード例 #1
0
        private static ICredentials GetBasicCredentials(Uri uri, bool forcePrompt)
        {
            string proxyHost = uri.Host;

            ICredentials basicCredentials = null;

            using (PromptForCredential dialog = new PromptForCredential()) {
                dialog.TargetName = string.Format(CultureInfo.InvariantCulture, "PackageExplorer_{0}", proxyHost);
                dialog.Title = string.Format(CultureInfo.CurrentCulture, "Connect to {0}", proxyHost);
                dialog.Message = dialog.Title;
                dialog.GenericCredentials = true;
                dialog.AlwaysShowUI = forcePrompt;
                if (dialog.ShowDialog()) {
                    basicCredentials = new NetworkCredential(dialog.UserName, dialog.Password);
                }
            }

            return basicCredentials;
        }
コード例 #2
0
        private static ICredentials GetBasicCredentials(Uri uri, bool forcePrompt)
        {
            string proxyHost = uri.Host;

            ICredentials basicCredentials = null;

            using (PromptForCredential dialog = new PromptForCredential()) {
                dialog.TargetName         = string.Format(CultureInfo.InvariantCulture, "PackageExplorer_{0}", proxyHost);
                dialog.Title              = string.Format(CultureInfo.CurrentCulture, "Connect to {0}", proxyHost);
                dialog.Message            = dialog.Title;
                dialog.GenericCredentials = true;
                dialog.AlwaysShowUI       = forcePrompt;
                if (dialog.ShowDialog())
                {
                    basicCredentials = new NetworkCredential(dialog.UserName, dialog.Password);
                }
            }

            return(basicCredentials);
        }
コード例 #3
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 PromptDialogResult ShowPromptForCredentialsDialog(System.Windows.Forms.IWin32Window owner, PromptDialogOptions options)
        {
            PromptDialogResult 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 PromptDialogResult();

                        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;
            }
        }