Exemple #1
0
        /// <summary>
        /// Prompts for password.
        /// </summary>
        /// <param name="user">The user, can specify an existing value</param>
        /// <param name="password">The password.</param>
        /// <returns>The authentication credentials result</returns>
        public static ResolveCredentialsResult PromptForPassword(SecurityPrincipal principal)
        {
            // Setup the flags and variables
            StringBuilder userPassword = new StringBuilder(100);
            StringBuilder userID = new StringBuilder(100);
            CREDUI_INFO credUI = new CREDUI_INFO();
            credUI.cbSize = Marshal.SizeOf(credUI);
            CREDUI_FLAGS flags = CREDUI_FLAGS.ALWAYS_SHOW_UI | CREDUI_FLAGS.GENERIC_CREDENTIALS;
            bool save = false;

            // Prompt the user
            CredUIReturnCodes returnCode = CredUIPromptForCredentials(ref credUI, String.Format("{0}@{1}", principal.Name, principal.Realm),
                IntPtr.Zero, 0, userID, 100, userPassword, 100, ref save, flags);

            if(returnCode == CredUIReturnCodes.NO_ERROR)
            {
                AuthenticationCredentials creds = new AuthenticationCredentials();
                string domain = String.Empty;
                string username = userID.ToString();
                string password = userPassword.ToString();

                if(username.Contains("\\"))
                {
                    string[] s = username.Split('\\');

                    username = s[0];
                    domain = s[1];
                }

                creds.Username = username;
                creds.Domain = domain;
                creds.Password = password;

                return new ResolveCredentialsResult(creds, save);
            }
            else
            {
                return null;
            }
        }
        private void addCredentialToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (CredentialEditorForm frm = new CredentialEditorForm(CheckPrincipal))
            {
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    AuthenticationCredentials creds = new AuthenticationCredentials(frm.Username, frm.Domain, frm.Password);

                    _credentials[frm.Principal] = creds;
                    UpdateCredentials();
                    OnCredentialsUpdated();
                }
            }
        }
        private void editCredentialToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (listViewCredentials.SelectedItems.Count > 0)
            {
                KeyValuePair<SecurityPrincipal, ICredentialObject> pair = (KeyValuePair<SecurityPrincipal, ICredentialObject>)listViewCredentials.SelectedItems[0].Tag;
                using (CredentialEditorForm frm = new CredentialEditorForm((p) => !p.Equals(pair.Key) && CheckPrincipal(p)))
                {
                    frm.Principal = pair.Key;
                    AuthenticationCredentials c = pair.Value as AuthenticationCredentials;
                    frm.Username = c.Username;
                    frm.Password = c.Password;
                    frm.Domain = c.Domain;

                    if (frm.ShowDialog(this) == DialogResult.OK)
                    {
                        SecurityPrincipal p = frm.Principal;

                        AuthenticationCredentials creds = new AuthenticationCredentials(frm.Username, frm.Domain, frm.Password);

                        _credentials[p] = creds;
                        UpdateCredentials();
                        OnCredentialsUpdated();
                    }
                }
            }
        }
 private void AddCredential(SecurityPrincipal principal, AuthenticationCredentials creds)
 {
     ListViewItem item = new ListViewItem(String.Format("{0}@{1}", principal.Name, principal.Realm));
     item.SubItems.Add(creds.Username);
     item.SubItems.Add(creds.Domain);
 }
        private void ResolveCredentials(object sender, ResolveCredentialsEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new EventHandler<ResolveCredentialsEventArgs>(ResolveCredentials), sender, e);
            }
            else
            {
                if (e.Principal.PrincipalType == typeof(AuthenticationCredentials))
                {
                    if (_document.Credentials.ContainsKey(e.Principal))
                    {
                        e.Result = new ResolveCredentialsResult(_document.Credentials[e.Principal], false);
                    }
                    else
                    {
                        using (GetAuthenticationCredentialsForm frm = new GetAuthenticationCredentialsForm(e.Principal))
                        {
                            if (frm.ShowDialog(this) == DialogResult.OK)
                            {
                                AuthenticationCredentials creds = new AuthenticationCredentials(frm.Username, frm.Domain, frm.Password);

                                e.Result = new ResolveCredentialsResult(creds, frm.SaveCreds && frm.SessionOnly);

                                if (frm.SaveCreds && !frm.SessionOnly)
                                {
                                    _document.Credentials[e.Principal] = creds;
                                    credentialsEditorControl.UpdateCredentials();
                                }
                            }
                        }
                    }
                }
            }
        }