Exemple #1
0
        public static CredUIReturnValues ShowPromptForWindowsCredentials(CredentialUI_Info credui, CredUIPromptFlags flags, out NetworkCredential cred)
        {
            if (IsWinVistaOrHigher())
            {
                throw new InvalidOperatingSystemException("Windows XP");
            }

            credui.cbSize = Marshal.SizeOf(credui);
            uint   authPackage   = 0;
            IntPtr outCredBuffer = new IntPtr();
            uint   outCredSize;
            bool   save = false;

            int result = WindowsSecurityNative.CredUIPromptForWindowsCredentials(ref credui,
                                                                                 0,
                                                                                 ref authPackage,
                                                                                 IntPtr.Zero,
                                                                                 0,
                                                                                 out outCredBuffer,
                                                                                 out outCredSize,
                                                                                 ref save,
                                                                                 (int)flags);

            cred = null;

            if (result == 0)
            {
                var usernameBuf = new StringBuilder(100);
                var passwordBuf = new StringBuilder(100);
                var domainBuf   = new StringBuilder(100);

                int maxUserName = 100;
                int maxDomain   = 100;
                int maxPassword = 100;
                if (WindowsSecurityNative.CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, usernameBuf, ref maxUserName,
                                                                         domainBuf, ref maxDomain, passwordBuf, ref maxPassword))
                {
                    //TODO: ms documentation says we should call this but i can't get it to work
                    //SecureZeroMem(outCredBuffer, outCredSize);

                    //clear the memory allocated by CredUIPromptForWindowsCredentials
                    WindowsSecurityNative.CoTaskMemFree(outCredBuffer);
                    cred = new NetworkCredential()
                    {
                        UserName = usernameBuf.ToString(),
                        Password = passwordBuf.ToString(),
                        Domain   = domainBuf.ToString()
                    };
                }
            }

            return((CredUIReturnValues)result);
        }
        public static CredUIReturnValues ShowPromptForWindowsCredentials(CredentialUI_Info credui, CredUIPromptFlags flags, out NetworkCredential cred)
        {
            if (IsWinVistaOrHigher())
                throw new InvalidOperatingSystemException("Windows XP");

            credui.cbSize = Marshal.SizeOf(credui);
            uint authPackage = 0;
            IntPtr outCredBuffer = new IntPtr();
            uint outCredSize;
            bool save = false;

            int result = WindowsSecurityNative.CredUIPromptForWindowsCredentials(ref credui,
                                                           0,
                                                           ref authPackage,
                                                           IntPtr.Zero,
                                                           0,
                                                           out outCredBuffer,
                                                           out outCredSize,
                                                           ref save,
                                                           (int)flags);

            cred = null;

            if (result == 0)
            {
                var usernameBuf = new StringBuilder(100);
                var passwordBuf = new StringBuilder(100);
                var domainBuf = new StringBuilder(100);

                int maxUserName = 100;
                int maxDomain = 100;
                int maxPassword = 100;
                if (WindowsSecurityNative.CredUnPackAuthenticationBuffer(0, outCredBuffer, outCredSize, usernameBuf, ref maxUserName,
                                                   domainBuf, ref maxDomain, passwordBuf, ref maxPassword))
                {
                    //TODO: ms documentation says we should call this but i can't get it to work
                    //SecureZeroMem(outCredBuffer, outCredSize);

                    //clear the memory allocated by CredUIPromptForWindowsCredentials
                    WindowsSecurityNative.CoTaskMemFree(outCredBuffer);
                    cred = new NetworkCredential()
                    {
                        UserName = usernameBuf.ToString(),
                        Password = passwordBuf.ToString(),
                        Domain = domainBuf.ToString()
                    };
                }
            }

            return (CredUIReturnValues)result;
        }