Exemplo n.º 1
0
        /// <summary>
        /// 演示如何弹出凭据提示以便输入用户凭据.
        /// </summary>
        private void btnCredentialUIPrompt_Click(object sender, EventArgs e)
        {
            try
            {
                using (Kerr.PromptForCredential dialog = new Kerr.PromptForCredential())
                {
                    dialog.Title              = "请指定用户";
                    dialog.DoNotPersist       = true;
                    dialog.ShowSaveCheckBox   = false;
                    dialog.TargetName         = Environment.MachineName;
                    dialog.ExpectConfirmation = true;

                    if (DialogResult.OK == dialog.ShowDialog(this))
                    {
                        tbPassword.Text = SecureStringToString(dialog.Password);
                        string[] strSplit = dialog.UserName.Split('\\');
                        if (strSplit.Length == 2)
                        {
                            this.tbUserName.Text = strSplit[1];
                            this.tbDomain.Text   = strSplit[0];
                        }
                        else
                        {
                            this.tbUserName.Text = dialog.UserName;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error");
            }
        }
        /// <summary>
        /// Prompts the user for credentials using a Windows dialog.
        /// </summary>
        private void PromptForCredentials()
        {
            using (Kerr.PromptForCredential prompt = new Kerr.PromptForCredential())
            {
                prompt.TargetName         = this.AuthenticationTarget;
                prompt.Title              = "Authorize Twitter client";
                prompt.Message            = "Enter your Twitter network credentials to authorize this application to read and post your updates.";
                prompt.UserName           = this.UserName ?? string.Empty;
                prompt.ExpectConfirmation = true;
                //prompt.Persist = false;
                prompt.GenericCredentials = true;

                if (prompt.ShowDialog(this.ownerWindow) == DialogResult.OK)
                {
                    try
                    {
                        ValidateCredentials(prompt.UserName, prompt.Password.ToUnsecureString());
                        this.UserName = prompt.UserName;
                        this.Password = prompt.Password.ToUnsecureString();
                        if (prompt.SaveChecked)
                        {
                            prompt.ConfirmCredentials();
                        }
                    }
                    catch (WebException)
                    {
                        // Make sure that if these were cached credentials that we clear them.
                        this.ClearCachedCredentials();
                        throw; // TODO: wrap exception appropriately.
                    }
                }
                else
                {
                    throw new OperationCanceledException();
                }
            }
        }
        private void btnCredentials_Click(object sender, EventArgs e)
        {
            using (Kerr.PromptForCredential dialog = new Kerr.PromptForCredential())
            {
                if (txtServer.Text!="")
                {
                    string server;
                    server = txtServer.Text.Replace(',', ':');
                    dialog.TargetName = server;
                    dialog.Message = "Please enter your SBI login information and it will be added to the Windows credential vault";
                    dialog.UserName = txtUname.Text;

                    dialog.ExpectConfirmation = true;

                    if (DialogResult.OK == dialog.ShowDialog(this))
                    {
                        if (dialog.SaveChecked)
                        {
                            dialog.ConfirmCredentials();
                        }
                    }
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 演示如何弹出凭据提示以便输入用户凭据.
        /// </summary>
        private void btnCredentialUIPrompt_Click(object sender, EventArgs e)
        {
            try
            {
                using (Kerr.PromptForCredential dialog = new Kerr.PromptForCredential())
                {
                    dialog.Title = "请指定用户";
                    dialog.DoNotPersist = true;
                    dialog.ShowSaveCheckBox = false;
                    dialog.TargetName = Environment.MachineName;
                    dialog.ExpectConfirmation = true;

                    if (DialogResult.OK == dialog.ShowDialog(this))
                    {
                        tbPassword.Text = SecureStringToString(dialog.Password);
                        string[] strSplit = dialog.UserName.Split('\\');
                        if (strSplit.Length == 2)
                        {
                            this.tbUserName.Text = strSplit[1];
                            this.tbDomain.Text = strSplit[0];
                        }
                        else
                        {
                            this.tbUserName.Text = dialog.UserName;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error");
            }
        }
        /// <summary>
        /// Prompts the user for credentials using a Windows dialog.
        /// </summary>
        private void PromptForCredentials()
        {
            using (Kerr.PromptForCredential prompt = new Kerr.PromptForCredential())
            {
                prompt.TargetName = this.AuthenticationTarget;
                prompt.Title = "Authorize Twitter client";
                prompt.Message = "Enter your Twitter network credentials to authorize this application to read and post your updates.";
                prompt.UserName = this.UserName ?? string.Empty;
                prompt.ExpectConfirmation = true;
                //prompt.Persist = false;
                prompt.GenericCredentials = true;

                if (prompt.ShowDialog(this.ownerWindow) == DialogResult.OK)
                {
                    try
                    {
                        ValidateCredentials(prompt.UserName, prompt.Password.ToUnsecureString());
                        this.UserName = prompt.UserName;
                        this.Password = prompt.Password.ToUnsecureString();
                        if (prompt.SaveChecked)
                        {
                            prompt.ConfirmCredentials();
                        }
                    }
                    catch (WebException)
                    {
                        // Make sure that if these were cached credentials that we clear them.
                        this.ClearCachedCredentials();
                        throw; // TODO: wrap exception appropriately.
                    }
                }
                else
                {
                    throw new OperationCanceledException();
                }
            }
        }