Exemple #1
0
        private static NetworkCredential UnpackAuthenticationBuffer([NotNull] SafeNativeMemory outCredBuffer)
        {
            Contract.Requires(outCredBuffer != null);

            const int maxLen = 100;

            var usernameBuf = new StringBuilder(maxLen);
            var passwordBuf = new StringBuilder(maxLen);
            var domainBuf   = new StringBuilder(maxLen);

            var maxUserLen     = maxLen;
            var maxDomainLen   = maxLen;
            var maxPasswordLen = maxLen;

            if (!NativeMethods.CredUnPackAuthenticationBuffer(1, outCredBuffer.DangerousGetHandle(), outCredBuffer.Size, usernameBuf, ref maxUserLen, domainBuf, ref maxDomainLen, passwordBuf, ref maxPasswordLen))
            {
                return(null);
            }

            return(new NetworkCredential()
            {
                UserName = usernameBuf.ToString(),
                Password = passwordBuf.ToString(),
                Domain = domainBuf.ToString()
            });
        }
Exemple #2
0
            public static bool GetTokenInformation([NotNull] SafeTokenHandle hToken, TOKEN_INFORMATION_CLASS tokenInfoClass, [NotNull] SafeNativeMemory pTokenInfo)
            {
                Contract.Requires(hToken != null);

                Contract.Requires(pTokenInfo != null);

                return(GetTokenInformation(hToken, tokenInfoClass, pTokenInfo.DangerousGetHandle(), pTokenInfo.Size, out _));
            }
        private static SafeNativeMemory PackAuthenticationBuffer([CanBeNull] NetworkCredential?credential)
        {
            var userName = credential?.UserName;

            if (string.IsNullOrEmpty(userName))
            {
                return(new SafeNativeMemory());
            }

            var inCredSize = 0;

            NativeMethods.CredPackAuthenticationBuffer(0, userName, string.Empty, IntPtr.Zero, ref inCredSize);
            var buffer = new SafeNativeMemory(inCredSize);

            NativeMethods.CredPackAuthenticationBuffer(0, userName, string.Empty, buffer.DangerousGetHandle(), ref inCredSize);
            return(buffer);
        }
        private static SafeNativeMemory PackAuthenticationBuffer(NetworkCredential credential)
        {
            var userName = credential.Maybe().Return(c => c.UserName);

            if (string.IsNullOrEmpty(userName))
                return new SafeNativeMemory();

            var inCredSize = 0;
            NativeMethods.CredPackAuthenticationBuffer(0, userName, string.Empty, IntPtr.Zero, ref inCredSize);
            var buffer = new SafeNativeMemory(inCredSize);
            NativeMethods.CredPackAuthenticationBuffer(0, userName, string.Empty, buffer.DangerousGetHandle(), ref inCredSize);
            return buffer;
        }
Exemple #5
0
        private static NetworkCredential PromptForCredential([NotNull] IWin32Window parent, [CanBeNull] string caption, [CanBeNull] string message, int authenticationError, [NotNull] SafeNativeMemory inCredBuffer)
        {
            Contract.Requires(parent != null);
            Contract.Requires(inCredBuffer != null);

            var  save        = true;
            uint authPackage = 0;

            var credui = new CREDUI_INFO
            {
                cbSize         = Marshal.SizeOf(typeof(CREDUI_INFO)),
                pszCaptionText = caption,
                pszMessageText = message,
                hwndParent     = parent.Handle
            };

            if (0 != NativeMethods.CredUIPromptForWindowsCredentials(ref credui, authenticationError, ref authPackage, inCredBuffer.DangerousGetHandle(), inCredBuffer.Size, out var outCredBufferPtr, out int outCredSize, ref save, 0))
            {
                return(null);
            }

            using (var outCredBuffer = new SafeNativeMemory(outCredBufferPtr, outCredSize))
            {
                return(UnpackAuthenticationBuffer(outCredBuffer));
            }
        }
Exemple #6
0
 public static bool GetTokenInformation(SafeTokenHandle hToken, TOKEN_INFORMATION_CLASS tokenInfoClass, SafeNativeMemory pTokenInfo)
 {
     return(GetTokenInformation(hToken, tokenInfoClass, pTokenInfo.DangerousGetHandle(), pTokenInfo.Size, out _));
 }
Exemple #7
0
        private static NetworkCredential?PromptForCredential(IntPtr parentHandle, string?caption, string?message, int authenticationError, SafeNativeMemory inCredBuffer)
        {
            var  save        = true;
            uint authPackage = 0;

            var credui = new CREDUI_INFO
            {
                cbSize         = Marshal.SizeOf(typeof(CREDUI_INFO)),
                pszCaptionText = caption,
                pszMessageText = message,
                hwndParent     = parentHandle
            };

            if (0 != NativeMethods.CredUIPromptForWindowsCredentials(ref credui, authenticationError, ref authPackage, inCredBuffer.DangerousGetHandle(), inCredBuffer.Size, out var outCredBufferPtr, out int outCredSize, ref save, 0))
            {
                return(null);
            }

            using var outCredBuffer = new SafeNativeMemory(outCredBufferPtr, outCredSize);
            return(UnpackAuthenticationBuffer(outCredBuffer));
        }