0
votes

I have a WPF WebBrowser control. Users log into a website using it and a session is created. When users click on links that create popups a new browser is launched (and the session is lost) so they are forced to re-authenticate.

I have worked around this by specifying an event handler to intercept the newWindow2 event:

Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
IServiceProvider serviceProvider = (IServiceProvider)webBrowser.Document;             
Guid serviceGuid = SID_SWebBrowserApp;
Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;            
SHDocVw.IWebBrowser2 myWebBrowser2 = (SHDocVw.IWebBrowser2) serviceProvider.QueryService(ref serviceGuid, ref iid);            
SHDocVw.DWebBrowserEvents2_Event wbEvents2 =(SHDocVw.DWebBrowserEvents2_Event)myWebBrowser2;           
wbEvents2.NewWindow2 += new SHDocVw.DWebBrowserEvents2_NewWindow2EventHandler(wbEvents2_NewWindow2);

The event handler code is as follows:

public void wbEvents2_NewWindow2(ref object ppDisp, ref bool Cancel)
{
        popUpWindow = new Window1();
        popUpWindow.Show();

        popUpWindow.webBrowser.Navigate(new Uri("about:blank"));
        Guid SID_SWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
        IServiceProvider serviceProvider = (IServiceProvider)popUpWindow.webBrowser.Document;

        Guid serviceGuid = SID_SWebBrowserApp;

        Guid iid = typeof(SHDocVw.IWebBrowser2).GUID;

        SHDocVw.IWebBrowser2 myWebBrowser2 = (SHDocVw.IWebBrowser2)serviceProvider.QueryService(ref serviceGuid, ref iid);

        ppDisp = myWebBrowser2.Application;

    }

Everything works great until certain JavaScript snippets get involved. For example if a link that generates a popup points to a JavaScript function in this fashion:

<a href="javascript:SomeFunction('someParameter','someParameter')">Some link here</a>

And the code within that function happens to open the window, then set a variable equal to the html document and attempts to write to the document, nothing ever gets written to the document in the WebBrowser control. For example:

Var someVar = window.open(“”,”Parameters”,”Parameters”);
Var someDocument = someVar.document;
someDocument.writeln(“This never ends up in the source of the page loaded in the WebBrowser control”);

If I comment out my code that intercepts the newWindow2 event and allow a new browser to launch and reauthenticate my user - the popup loads as expected (the JavaScript writeln functions write data to the document).

I was hoping someone could explain to me why the JavaScript writeln’s are not being written to the document that is displayed in the WPF WebBrowser control, and/or suggest a way of doing so.

1
I have created a small messy POC project that demonstrates this issue. It can be downloaded from this link. After it is run, type http://www.budwill.net/test/ into the textbox and click Go. A page appears with a link. The link calls a JavaScript function that executes window.open and then some document.write's. The new popup window appears blank. However if you were to visit the same URL in a regular browser and click the link, the popup is populated with the text from document.write's. - Andrew

1 Answers

0
votes

Have you tried a different GUID for the browser?