public ICredentials GetCredentials(Uri uri, IWebProxy proxy, CredentialType credentialType, ICredentials existingCredential, bool retrying)
        {
            if (uri == null)
            {
                throw new ArgumentNullException("uri");
            }

            NetworkCredential currentCredentials = null;

            if (existingCredential != null)
            {
                currentCredentials = Utility.GetCredentialsForUriFromICredentials(uri, existingCredential);
            }

            var form   = new PlaceholderForm(credentialType, uri, currentCredentials);
            var result = GdkWin32.RunModalWin32Form(form, IdeApp.Workbench.RootWindow);

            return(result ? new NetworkCredential(form.Username, form.Password, form.Domain) : null);
        }
        static ICredentials GetCredentialsFromUser(Uri uri, IWebProxy proxy, CredentialType credentialType)
        {
            NetworkCredential result = null;

            Runtime.RunInMainThread(() => {
                var form = new PlaceholderForm(credentialType, uri, null);
                if (GdkWin32.RunModalWin32Form(form, IdeApp.Workbench.RootWindow))
                {
                    result = new NetworkCredential(form.Username, form.Password, form.Domain);
                }
            }).Wait();

            // store the obtained credentials in the auth store
            // but don't store for the root url since it may have other credentials
            if (result != null)
            {
                PasswordService.AddWebUserNameAndPassword(uri, result.UserName, result.Password);
            }

            return(result);
        }
        public override DialogResult ShowMagicDialog()
        {
            var message = type == CredentialType.ProxyCredentials
                                ? GettextCatalog.GetString(
                "{0} needs credentials to access the proxy server {1}.",
                BrandingService.ApplicationName,
                uri.Host
                )
                                : GettextCatalog.GetString(
                "{0} needs credentials to access {1}.",
                BrandingService.ApplicationName,
                uri.Host
                );

            var credUiInfo = new Native.CredentialUiInfo {
                MessageText   = message,
                CaptionText   = GettextCatalog.GetString("Credentials Required"),
                StructureSize = Marshal.SizeOf(typeof(Native.CredentialUiInfo)),
                ParentWindow  = GdkWin32.HgdiobjGet(IdeApp.Workbench.RootWindow.GdkWindow)
            };

            var save = false;

            StringBuilder username = new StringBuilder(current != null ? current.UserName : string.Empty, 100),
                          password = new StringBuilder(current != null ? current.Password : string.Empty, 100),
                          domain = new StringBuilder(100);
            int maxUsername = 100, maxPassword = 100, maxDomain = 100;

            var windowsVersion = Environment.OSVersion.Version;

            // Vista or higher = 6.x+, XP = 5.x
            if (windowsVersion.Major >= 6)
            {
                int    outputSize, authPackage = 0;
                IntPtr output;

                var  pinnedArray            = new GCHandle();
                uint packedAuthBufferLength = 0;

                if (current != null)
                {
                    // Have creds? Pack them into the buffer and predisplay them.
                    const int credPackGenericCredentials = 4;
                    Native.CredPackAuthenticationBuffer(credPackGenericCredentials, current.UserName, current.Password,
                                                        IntPtr.Zero, ref packedAuthBufferLength);
                    // Now we know the size of the buffer, allocate a byte[] and pin it
                    var packedAuthBufferBytes = new byte[packedAuthBufferLength];
                    // Free the dummy handle from before
                    if (pinnedArray.IsAllocated)
                    {
                        pinnedArray.Free();
                    }
                    pinnedArray = GCHandle.Alloc(packedAuthBufferBytes, GCHandleType.Pinned);
                    Native.CredPackAuthenticationBuffer(credPackGenericCredentials, current.UserName, current.Password, pinnedArray.AddrOfPinnedObject(), ref packedAuthBufferLength);
                }

                var authBuffer = current == null ? IntPtr.Zero : pinnedArray.AddrOfPinnedObject();;
                var returnCode = Native.CredUIPromptForWindowsCredentials(ref credUiInfo, 0, ref authPackage, authBuffer, packedAuthBufferLength,
                                                                          out output, out outputSize, ref save, Native.CredentialsUiWindowsFlags.Generic);

                // Unpin the array if we held it before
                if (authBuffer != IntPtr.Zero)
                {
                    pinnedArray.Free();
                }

                if (returnCode != Native.WindowsCredentialPromptReturnCode.NoError)
                {
                    return(DialogResult.Cancel);
                }

                if (!Native.CredUnPackAuthenticationBuffer(0, output, outputSize, username, ref maxUsername, domain, ref maxDomain, password, ref maxPassword))
                {
                    return(DialogResult.Cancel);
                }

                Native.CoTaskMemFree(output);

                Username = username.ToString();
                Password = password.ToString();
                Domain   = domain.ToString();

                return(DialogResult.OK);
            }
            else
            {
                const Native.CredentialsUiFlags flags = Native.CredentialsUiFlags.AlwaysShowUi | Native.CredentialsUiFlags.GenericCredentials;
                var returnCode = Native.CredUIPromptForCredentials(ref credUiInfo, BrandingService.ApplicationName, IntPtr.Zero, 0,
                                                                   username, maxUsername, password, maxPassword, ref save, flags);
                Username = username.ToString();
                Password = password.ToString();
                Domain   = string.Empty;

                return(returnCode == Native.CredUiReturnCodes.NoError ? DialogResult.OK : DialogResult.Cancel);
            }
        }