コード例 #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()));
        }
コード例 #2
0
        private static NativeMethods.CredUIReturnCodes SspiPromptForCredentials(NativeMethods.CREDUI_INFO credUiInfo, Uri targetUri, ref bool pfSave, out string userName, out SecureString password)
        {
            IntPtr zero = IntPtr.Zero;

            NativeMethods.CredUIReturnCodes credUIReturnCodes = NativeMethods.SspiPromptForCredentials(targetUri.Host, ref credUiInfo, 0U, "Negotiate", IntPtr.Zero, ref zero, ref pfSave, 1);
            if (credUIReturnCodes == null)
            {
                try
                {
                    CredentialHelper.AuthIdentityToCredential(zero, out userName, out password);
                    return(credUIReturnCodes);
                }
                finally
                {
                    NativeMethods.SspiFreeAuthIdentity(zero);
                }
            }
            userName = null;
            password = null;
            return(credUIReturnCodes);
        }