protected internal virtual void OnCredentialsSupported(GitCredentialsEventArgs e) { var ev = CredentialsSupported; if (ev != null) ev(this, e); }
void _clientArgs_CredentialsSupported(object sender, GitCredentialsEventArgs e) { Invoke( new Action<GitCredentialsEventArgs, bool>(ProcessCredentials), e, true ); }
private void ProcessCredentials(GitCredentialsEventArgs e, bool checkSupports) { if (checkSupports) { foreach (var item in e.Items) { switch (item.Type) { case GitCredentialsType.Informational: case GitCredentialsType.Password: case GitCredentialsType.String: case GitCredentialsType.Username: case GitCredentialsType.YesNo: break; default: e.Cancel = true; break; } } } else { // First, see whether: // * We are processing a username/password combination; // * We can already process the information and yes/no types; // * All credentials are already present in cache. GitCredentialItem usernameItem = null; GitCredentialItem passwordItem = null; bool oneMissing = false; foreach (var item in e.Items) { if (!ProcessCached(e.Uri, item)) { oneMissing = true; switch (item.Type) { case GitCredentialsType.Username: usernameItem = item; break; case GitCredentialsType.Password: passwordItem = item; break; case GitCredentialsType.Informational: if (!ShowInformation(item)) _canceling = true; break; case GitCredentialsType.YesNo: if (!ShowYesNo(item)) _canceling = true; break; } if (_canceling) { e.Cancel = true; return; } } } if (!oneMissing) return; // When we have both a username and password, we display a // combined dialog for this one. bool hadUsernamePassword = usernameItem != null && passwordItem != null; bool rememberPassword = false; if (hadUsernamePassword) { string description = String.Format(Properties.Resources.TheServerXRequiresAUsernameAndPassword, e.Uri); if (Environment.OSVersion.Platform == PlatformID.Win32NT && Environment.OSVersion.Version >= new Version(5, 1)) { // If Windows XP/Windows 2003 or higher: Use the windows password dialog GetUserNamePasswordWindows(e, description, usernameItem, passwordItem, ref rememberPassword); if (e.Cancel) return; } else { using (UsernamePasswordCredentialsDialog dialog = new UsernamePasswordCredentialsDialog()) { dialog.UsernameItem = usernameItem; dialog.PasswordItem = passwordItem; dialog.RememberPassword = rememberPassword; if (dialog.ShowDialog(Context, this) != DialogResult.OK) { e.Cancel = true; return; } rememberPassword = dialog.RememberPassword; } } } // Process the rest. These are processed in a generic dialog. // Only when the type is Username, will the dialog not be // displayed as a password dialog. foreach (var item in e.Items) { switch (item.Type) { case GitCredentialsType.Informational: case GitCredentialsType.YesNo: // Already processed these above. break; case GitCredentialsType.Username: case GitCredentialsType.Password: rememberPassword = item.Type == GitCredentialsType.Password; if (!hadUsernamePassword) { if (!ShowGeneric(item, ref rememberPassword)) _canceling = true; } break; default: rememberPassword = item.Type != GitCredentialsType.Username; if (!ShowGeneric(item, ref rememberPassword)) _canceling = true; break; } if (_canceling) { e.Cancel = true; return; } } if (rememberPassword) { foreach (var item in e.Items) { UpdateCache(e.Uri, item); } } } }
private void GetUserNamePasswordWindows(GitCredentialsEventArgs e, string description, GitCredentialItem usernameItem, GitCredentialItem passwordItem, ref bool rememberPassword) { NativeMethods.CREDUI_INFO info = new NativeMethods.CREDUI_INFO(); info.pszCaptionText = Properties.Resources.ConnectToGit; info.pszMessageText = description; info.hwndParent = IntPtr.Zero; info.cbSize = Marshal.SizeOf(typeof(NativeMethods.CREDUI_INFO)); StringBuilder sbUserName = new StringBuilder("", 1024); StringBuilder sbPassword = new StringBuilder("", 1024); var flags = NativeMethods.CREDUI_FLAGS.GENERIC_CREDENTIALS | NativeMethods.CREDUI_FLAGS.ALWAYS_SHOW_UI | NativeMethods.CREDUI_FLAGS.DO_NOT_PERSIST | NativeMethods.CREDUI_FLAGS.SHOW_SAVE_CHECK_BOX; var result = NativeMethods.CredUIPromptForCredentials( ref info, e.Uri, IntPtr.Zero, 0, sbUserName, 1024, sbPassword, 1024, ref rememberPassword, flags ); switch (result) { case NativeMethods.CredUIReturnCodes.NO_ERROR: usernameItem.Value = sbUserName.ToString(); passwordItem.Value = sbPassword.ToString(); break; case NativeMethods.CredUIReturnCodes.ERROR_CANCELLED: usernameItem.Value = null; passwordItem.Value = null; e.Cancel = true; break; } }