コード例 #1
0
        private bool PromptForCredentialsCredUI(IntPtr owner, bool storedCredentials)
        {
            NativeMethods.CREDUI_INFO info = CreateCredUIInfo(owner, true);
            NativeMethods.CREDUI_FLAGS flags = NativeMethods.CREDUI_FLAGS.GENERIC_CREDENTIALS | NativeMethods.CREDUI_FLAGS.DO_NOT_PERSIST | NativeMethods.CREDUI_FLAGS.ALWAYS_SHOW_UI;
            if( ShowSaveCheckBox )
                flags |= NativeMethods.CREDUI_FLAGS.SHOW_SAVE_CHECK_BOX;

            StringBuilder user = new StringBuilder(NativeMethods.CREDUI_MAX_USERNAME_LENGTH);
            user.Append(UserName);
            StringBuilder pw = new StringBuilder(NativeMethods.CREDUI_MAX_PASSWORD_LENGTH);
            pw.Append(Password);

            NativeMethods.CredUIReturnCodes result = NativeMethods.CredUIPromptForCredentials(ref info, Target, IntPtr.Zero, 0, user, NativeMethods.CREDUI_MAX_USERNAME_LENGTH, pw, NativeMethods.CREDUI_MAX_PASSWORD_LENGTH, ref _isSaveChecked, flags);
            switch( result )
            {
                case NativeMethods.CredUIReturnCodes.NO_ERROR:
                    UserName = user.ToString();
                    Password = pw.ToString();
                    if( ShowSaveCheckBox )
                    {
                        _confirmTarget = Target;
                        // If the credential was stored previously but the user has now cleared the save checkbox,
                        // we want to delete the credential.
                        if( storedCredentials && !IsSaveChecked )
                            DeleteCredential(Target);
                    }
                    return true;
                case NativeMethods.CredUIReturnCodes.ERROR_CANCELLED:
                    return false;
                default:
                    throw new CredentialException((int)result);
            }
        }
コード例 #2
0
 private NativeMethods.CREDUI_INFO CreateCredUIInfo(IntPtr owner, bool downlevelText)
 {
     NativeMethods.CREDUI_INFO info = new NativeMethods.CREDUI_INFO();
     info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
     info.hwndParent = owner;
     if( downlevelText )
     {
         info.pszCaptionText = WindowTitle;
         switch( DownlevelTextMode )
         {
         case DownlevelTextMode.MainInstructionAndContent:
             if( MainInstruction.Length == 0 )
                 info.pszMessageText = Content;
             else if( Content.Length == 0 )
                 info.pszMessageText = MainInstruction;
             else
                 info.pszMessageText = MainInstruction + Environment.NewLine + Environment.NewLine + Content;
             break;
         case DownlevelTextMode.MainInstructionOnly:
             info.pszMessageText = MainInstruction;
             break;
         case DownlevelTextMode.ContentOnly:
             info.pszMessageText = Content;
             break;
         }
     }
     else
     {
         // Vista and later don't use the window title.
         info.pszMessageText = Content;
         info.pszCaptionText = MainInstruction;
     }
     return info;
 }
コード例 #3
0
        private bool PromptForCredentialsCredUIWin(IntPtr owner, bool storedCredentials)
        {
            NativeMethods.CREDUI_INFO    info  = CreateCredUIInfo(owner, false);
            NativeMethods.CredUIWinFlags flags = NativeMethods.CredUIWinFlags.Generic;
            if (ShowSaveCheckBox)
            {
                flags |= NativeMethods.CredUIWinFlags.Checkbox;
            }

            IntPtr inBuffer  = IntPtr.Zero;
            IntPtr outBuffer = IntPtr.Zero;

            try
            {
                uint inBufferSize = 0;
                if (UserName.Length > 0)
                {
                    NativeMethods.CredPackAuthenticationBuffer(0, UserName, Password, IntPtr.Zero, ref inBufferSize);
                    if (inBufferSize > 0)
                    {
                        inBuffer = Marshal.AllocCoTaskMem((int)inBufferSize);
                        if (!NativeMethods.CredPackAuthenticationBuffer(0, UserName, Password, inBuffer, ref inBufferSize))
                        {
                            throw new CredentialException(Marshal.GetLastWin32Error());
                        }
                    }
                }

                uint outBufferSize;
                uint package = 0;
                NativeMethods.CredUIReturnCodes result = NativeMethods.CredUIPromptForWindowsCredentials(ref info, 0, ref package, inBuffer, inBufferSize, out outBuffer, out outBufferSize, ref _isSaveChecked, flags);
                switch (result)
                {
                case NativeMethods.CredUIReturnCodes.NO_ERROR:
                    StringBuilder userName     = new StringBuilder(NativeMethods.CREDUI_MAX_USERNAME_LENGTH);
                    StringBuilder password     = new StringBuilder(NativeMethods.CREDUI_MAX_PASSWORD_LENGTH);
                    uint          userNameSize = (uint)userName.Capacity;
                    uint          passwordSize = (uint)password.Capacity;
                    uint          domainSize   = 0;
                    if (!NativeMethods.CredUnPackAuthenticationBuffer(0, outBuffer, outBufferSize, userName, ref userNameSize, null, ref domainSize, password, ref passwordSize))
                    {
                        throw new CredentialException(Marshal.GetLastWin32Error());
                    }
                    UserName = userName.ToString();
                    Password = password.ToString();
                    if (ShowSaveCheckBox)
                    {
                        _confirmTarget = Target;
                        // If the credential was stored previously but the user has now cleared the save checkbox,
                        // we want to delete the credential.
                        if (storedCredentials && !IsSaveChecked)
                        {
                            DeleteCredential(Target);
                        }
                    }
                    return(true);

                case NativeMethods.CredUIReturnCodes.ERROR_CANCELLED:
                    return(false);

                default:
                    throw new CredentialException((int)result);
                }
            }
            finally
            {
                if (inBuffer != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(inBuffer);
                }
                if (outBuffer != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(outBuffer);
                }
            }
        }
コード例 #4
0
 private NativeMethods.CREDUI_INFO CreateCredUIInfo(IntPtr owner, bool downlevelText)
 {
     NativeMethods.CREDUI_INFO info = new NativeMethods.CREDUI_INFO();
     info.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(info);
     info.hwndParent = owner;
     if( downlevelText )
     {
         info.pszCaptionText = WindowTitle;
         switch( DownlevelTextMode )
         {
         case DownlevelTextMode.MainInstructionAndContent:
             if( MainInstruction.Length == 0 )
                 info.pszMessageText = Content;
             else if( Content.Length == 0 )
                 info.pszMessageText = MainInstruction;
             else
                 info.pszMessageText = MainInstruction + Environment.NewLine + Environment.NewLine + Content;
             break;
         case DownlevelTextMode.MainInstructionOnly:
             info.pszMessageText = MainInstruction;
             break;
         case DownlevelTextMode.ContentOnly:
             info.pszMessageText = Content;
             break;
         }
     }
     else
     {
         // Vista and later don't use the window title.
         info.pszMessageText = Content;
         info.pszCaptionText = MainInstruction;
     }
     return info;
 }