views:

30

answers:

2

I recently upgraded an MSTest project to .NET 4.0 and VS 2010. Several of the tests query an outside vendor service and thus prompt the user for necessary credentials to communicate through our corporate web proxy. This used to work fine in vs2008 but after the upgrade the dialog will only display if the user switches focus from VS to another app immediately after kicking off the tests. Is there anything special that needs to be done when displaying the dialog? The best I can figure is that there is some WPF caveat that got introduced with the redesign of VS.

The code in question

private void PromptUser()
        {
            if (!credentialsSet)
            {
                using (CredentialsDialog dialog = new CredentialsDialog(true))
                {
                    Process process = Process.GetCurrentProcess();
                    IWin32Window window = Control.FromHandle(process.MainWindowHandle);
                    DialogResult dr = dialog.ShowDialog(window);

                    if (dr == DialogResult.Cancel)
                    {
                        throw new InvalidOperationException("Credentials not entered");
                    }

                    credentials = dialog.Credentials;
                    user = dialog.Username;
                    password = dialog.Password;
                    domain = dialog.Domain;
                }

                credentialsSet = true;
            }
        }
A: 

I would sugest that requiring the credentials is bad practice. Have you conidered adding your credentials in an excrypted fasion to the config of the test assembly.

You will run into a lot of problems if you try to setup this for automated build.

MrHinsh
@MrHinsh I agree with you that it is a bad practice, sadly it is a required one due to our vendor contract in that all requests have to come from the company's gateway and only a few people have internet access. My workaround is similar to your suggestion. I externalized the credentials into their own file as I did not want to check it into our SCM. However it has code smell manly from the fact that the password expires every 28 days so it becomes a procedural issue for the maintainer to update that file in order to not lock out the account and cause false test failures.
Tedford
@Tedford, MrHinsh: Not to mention you've got a password sitting there in a file on your HD, going across your network in plaintext, possibly sitting on network shares, etc.
Merlyn Morgan-Graham
A: 

You may need to activate the dialog in order to get it to pop up over whatever programs you have active.

using (CredentialsDialog dialog = new CredentialsDialog(true))
{
    Process process = Process.GetCurrentProcess();
    IWin32Window window = Control.FromHandle(process.MainWindowHandle);
    dialog.Activate();
    DialogResult dr = dialog.ShowDialog(window);

    // ...
}

http://msdn.microsoft.com/en-us/library/system.windows.forms.form.activate.aspx

Merlyn Morgan-Graham
@Merlyn Sorry for the delay in response but I wasn't able to test the suggestion until today. Adding dialog.Activate() changed the behavior but it is still far from desired. Now the dialog will display for the first run but subsequent runs still hang without displaying the dialog. Most likely this suggests the issue is some sort of WinForms/WPF integration hiccup.
Tedford