Exemple #1
0
        public static PSCredential PromptForCredentials(IntPtr parentWindow, string displayName, Uri targetUri, string errorMessage, ref bool pfSave)
        {
            string text = errorMessage;

            NativeMethods.CREDUI_INFO credUiInfo;
            credUiInfo..ctor(parentWindow);
            credUiInfo.pszCaptionText = Strings.CredentialInputDialogTitle(displayName);
            NativeMethods.CredUIReturnCodes credUIReturnCodes;
            for (;;)
            {
                credUiInfo.pszMessageText = (text ?? string.Empty);
                string       userName;
                SecureString password;
                credUIReturnCodes = (WinformsHelper.IsWin7OrLater() ? CredentialHelper.SspiPromptForCredentials(credUiInfo, targetUri, ref pfSave, out userName, out password) : CredentialHelper.CredUIPromptForCredentials(credUiInfo, ref pfSave, out userName, out password));
                if (credUIReturnCodes == null)
                {
                    try
                    {
                        return(new PSCredential(userName, password));
                    }
                    catch (PSArgumentException)
                    {
                        text = Strings.InvalidUserNameOrPassword;
                        continue;
                    }
                }
                if (credUIReturnCodes == 1223)
                {
                    break;
                }
                if (credUIReturnCodes != 1315)
                {
                    goto IL_8F;
                }
                text = Strings.InvalidUserNameOrPassword;
            }
            return(null);

IL_8F:
            throw new InvalidOperationException(string.Format("PromptForCredentials failed with error {0}.", credUIReturnCodes.ToString()));
        }
        private MonadConnectionInfo RetryCredentialsFromLoadOrInput(Func <PSCredential, MonadConnectionInfo> tryConnectWithCredential)
        {
            PSCredential cred = this.proposedCredential;

            if (cred == null && !string.IsNullOrEmpty(this.credentialKey))
            {
                cred = CredentialHelper.ReadCredential(this.credentialKey);
            }
            MonadConnectionInfo monadConnectionInfo = null;

            while (monadConnectionInfo == null)
            {
                if (cred == null && this.uiInteractionHandler != null)
                {
                    if (WinformsHelper.IsWin7OrLater() && this.orgType == OrganizationType.Cloud)
                    {
                        CredentialHelper.ForceConnection(this.uri);
                    }
                    this.uiInteractionHandler.DoActionSynchronizely(delegate(IWin32Window window)
                    {
                        cred = CredentialHelper.PromptForCredentials((window == null) ? IntPtr.Zero : window.Handle, this.displayName, this.uri, this.GetErrorMessage(), ref this.rememberCredentialChecked);
                    });
                    if (cred != null && !string.IsNullOrEmpty(this.credentialKey))
                    {
                        CredentialHelper.RemoveCredential(this.credentialKey);
                    }
                }
                if (cred == null)
                {
                    throw new OperationCanceledException(Strings.UnableToConnectExchangeForest(this.displayName));
                }
                monadConnectionInfo = tryConnectWithCredential(cred);
                cred = null;
            }
            return(monadConnectionInfo);
        }