Orc.NuGetExplorer.Native.CredUi.CredPackAuthenticationBuffer C# (CSharp) Method

CredPackAuthenticationBuffer() private method

private CredPackAuthenticationBuffer ( int dwFlags, string pszUserName, string pszPassword, IntPtr pPackedCredentials, uint &pcbPackedCredentials ) : bool
dwFlags int
pszUserName string
pszPassword string
pPackedCredentials System.IntPtr
pcbPackedCredentials uint
return bool
        public static extern bool CredPackAuthenticationBuffer(int dwFlags, string pszUserName, string pszPassword, IntPtr pPackedCredentials,
            ref uint pcbPackedCredentials);

Usage Example

        private bool PromptForCredentials(IntPtr owner, bool storedCredentials, ref IntPtr inBuffer, ref IntPtr outBuffer)
        {
            var info  = CreateCredUIInfo(owner, false);
            var flags = CredUi.PromptForWindowsCredentials.Generic;

            if (ShowSaveCheckBox)
            {
                flags |= CredUi.PromptForWindowsCredentials.Checkbox;
            }

            uint inBufferSize = 0;

            if (UserName.Length > 0)
            {
                // First call is only to get the required buffer size
                CredUi.CredPackAuthenticationBuffer(0, UserName, Password, IntPtr.Zero, ref inBufferSize);
                if (inBufferSize > 0)
                {
                    inBuffer = Marshal.AllocCoTaskMem((int)inBufferSize);
                    if (!CredUi.CredPackAuthenticationBuffer(0, UserName, Password, inBuffer, ref inBufferSize))
                    {
                        throw Log.ErrorAndCreateException(x => new CredentialException(Marshal.GetLastWin32Error()),
                                                          "Failed to create the authentication buffer before prompting");
                    }
                }
            }

            uint package = 0;

            Log.Debug("Prompting user for credentials");

            var result = CredUi.CredUIPromptForWindowsCredentials(ref info, 0, ref package, inBuffer, inBufferSize,
                                                                  out outBuffer, out var outBufferSize, ref _isSaveChecked, flags);

            switch (result)
            {
            case CredUi.CredUiReturnCodes.NO_ERROR:
                ManageCredentialsStorage(outBuffer, outBufferSize, storedCredentials);
                return(true);

            case CredUi.CredUiReturnCodes.ERROR_CANCELLED:
                Log.Debug("User canceled the credentials prompt");
                return(false);

            default:
                throw Log.ErrorAndCreateException(x => new CredentialException((int)result),
                                                  "Failed to prompt for credentials, error code '{0}'", result);
            }
        }
All Usage Examples Of Orc.NuGetExplorer.Native.CredUi::CredPackAuthenticationBuffer