示例#1
0
 internal static extern int CredUIPromptForWindowsCredentials(ref CredentialUI_Info notUsedHere,
                                                              int authError,
                                                              ref uint authPackage,
                                                              IntPtr InAuthBuffer,
                                                              uint InAuthBufferSize,
                                                              out IntPtr refOutAuthBuffer,
                                                              out uint refOutAuthBufferSize,
                                                              ref bool fSave,
                                                              int flags);
示例#2
0
        public static void GetCredentialsVistaAndUp(string captionText, string messageText, out NetworkCredential networkCredential, Form form = null)
        {
            CredentialUI_Info credui = new CredentialUI_Info();

            //credui.pszCaptionText = "Please enter the credentails for " + serverName;
            credui.pszCaptionText = captionText;
            credui.pszMessageText = messageText;
            if (form != null)
            {
                credui.hwndParent = form.Handle;
            }
            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,
                                                                                    1 /* Generic */);

            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 (result == 0)
            {
                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);
                    networkCredential = new NetworkCredential()
                    {
                        UserName = usernameBuf.ToString(),
                        Password = passwordBuf.ToString(),
                        Domain   = domainBuf.ToString()
                    };
                    return;
                }
            }

            networkCredential = null;
        }
示例#3
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 void GetCredentialsVistaAndUp(string captionText, string messageText, out NetworkCredential networkCredential, IntPtr pointer)
        {
            CredentialUI_Info credui = new CredentialUI_Info();
            //credui.pszCaptionText = "Please enter the credentails for " + serverName;
            credui.pszCaptionText = captionText;
            credui.pszMessageText = messageText;
            credui.hwndParent = pointer;
            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,
                                                           1 /* Generic */);

            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 (result == 0)
            {
                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);
                    networkCredential = new NetworkCredential()
                    {
                        UserName = usernameBuf.ToString(),
                        Password = passwordBuf.ToString(),
                        Domain = domainBuf.ToString()
                    };
                    return;
                }
            }

            networkCredential = null;
        }
 internal static extern int CredUIPromptForWindowsCredentials(ref CredentialUI_Info notUsedHere,
                                                              int authError,
                                                              ref uint authPackage,
                                                              IntPtr InAuthBuffer,
                                                              uint InAuthBufferSize,
                                                              out IntPtr refOutAuthBuffer,
                                                              out uint refOutAuthBufferSize,
                                                              ref bool fSave,
                                                              int flags);
        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;
        }