'WebBrowser Control with Google 2-Step Verification

We have a class library that uses the .NET WebBrowser control to authorize access to a third-party API, either with that third-party's oAuth2 Authorization flow or with Google 2-Step Verification (aka 2-factor/multi-factor authentication, etc).

The first flow works fine; the user is prompted to enter their email and password and click Confirm. No issues.

However the second flow dies once the user confirms (via their mobile device). The pop-up webpage in the WebBrowser control doesn't move to the verified page, it just sits there as if it's waiting for the user to verify.

Our code implements an event handler for the WebBrowserDocumentCompleted event and this is fired as expected whenever the page updates. It's not firing after the user confirms via their device, though.

I've had a pretty decent hunt around the web and SO for tips on what's going on, and I've also had a look at WebView2 and CefSharp but the latter requires an additional C++ library which I'd rather not bundle or have to worry about and the former is in a very new state of development after having spent several hours trying to just get a basic demo working (and failing).

So if it's at all possible, I'd like to nut out a solution which continues to use the WebBrowser control.

NOTE: I have checked which version is being used and it's 11 based on the Version property. I'm developing using VS 2019 on a Windows 10 Pro machine targeting .NET 4.5.2 (for maximum backwards compatibility).



Solution 1:[1]

Well, the solution actually was along the lines of some other posts, especially this one.

The clue was that 2SV worked fine in the resident IE browser on my machine.

In the end, I added a modified version of the code posted in the link above to my static initialization code:

private static void SetWebBrowserCompatibilityMode()
{
    uint iMode = 11001; // Default to Edge/IE11
    using (WebBrowser oBrowser = new WebBrowser())
    {
        switch (oBrowser.Version.Major)
        {
            case 10:
                iMode = 10001;
                break;
            case 9:
                iMode = 9999;
                break;
            case 8:
                iMode = 8888;
                break;
        }
    }
    using (RegistryKey oKey = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION", true))
    {
        oKey.SetValue(Path.GetFileName(Application.ExecutablePath), iMode, RegistryValueKind.DWord);
        oKey.Close();
    }
}

This isn't ideal as it requires a Registry change but being the CURRENT_USER hive there shouldn't be any elevated privileges required. And I saw that other applications had already added key entries, such as Outlook, SourceTree and Zoom.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SteveCinq