Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebBrowserControl: UnauthorizedAccessException when accessing property of a Frame

Tags:

c#

.net

vb.net

I coded a very small website bot in C# using the default WebBrowser control. Actually almost everything is working the way it is supposed to work, yet I seem to have problems with the very last step of my automation.

The website was built using several iframes. This isn't much of a big deal as I simply access those frames and their elements using

webBrowser1.Document.Window.Frames[0].Document.GetElementById("element").InvokeMember("click");

This however does not work when the source of the IFRAME is being hosted on a different domain than the actual website. As I searched the internet for an answer to my problem I stumbled across an MSDN article mentioning this specific problem and they were referring to safety measures against cross site scripting which might be the reason for this error.

I couldn't really find a way of disabling this feature so I moved on and decided to recode everything to work with geckofx-12 instead of the default (IE based) web browser control, yet I ran into similar issues...

My question is: Is there any way I can bypass this annoying behaviour? I don't really care about security concerns or on whether geckofx or the default web browser control is being used, I would just like to programmatically access the elements of a site which is being hosted on a different domain without running into an UnauthorizedAccessException.

I would love to get advice from the gurus out there.

like image 450
beta Avatar asked May 18 '12 00:05

beta


1 Answers

You can't access frames from different domains. That is a security feature. There is a little hack for it:

 public class CrossFrameIE
{
    // Returns null in case of failure.
    public static IHTMLDocument2 GetDocumentFromWindow(IHTMLWindow2 htmlWindow)
    {
        if (htmlWindow == null)
        {
            return null;
        }

        // First try the usual way to get the document.
        try
        {
            IHTMLDocument2 doc = htmlWindow.document;                

            return doc;
        }
        catch (COMException comEx)
        {
            // I think COMException won't be ever fired but just to be sure ...
            if (comEx.ErrorCode != E_ACCESSDENIED)
            {
                return null;
            }
        }
        catch (System.UnauthorizedAccessException)
        {
        }
        catch
        {
            // Any other error.
            return null;
        }

        // At this point the error was E_ACCESSDENIED because the frame contains a document from another domain.
        // IE tries to prevent a cross frame scripting security issue.
        try
        {
            // Convert IHTMLWindow2 to IWebBrowser2 using IServiceProvider.
            IServiceProvider sp = (IServiceProvider)htmlWindow;

            // Use IServiceProvider.QueryService to get IWebBrowser2 object.
            Object brws = null;
            sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out brws);

            // Get the document from IWebBrowser2.
            IWebBrowser2 browser = (IWebBrowser2)(brws);

            return (IHTMLDocument2)browser.Document;
        }
        catch
        {
        }

        return null;
    }

    private const int E_ACCESSDENIED = unchecked((int)0x80070005L);
    private static Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
    private static Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E");
}

// This is the COM IServiceProvider interface, not System.IServiceProvider .Net interface!
[ComImport(), ComVisible(true), Guid("6D5140C1-7436-11CE-8034-00AA006009FA"),
InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
public interface IServiceProvider
{
    [return: MarshalAs(UnmanagedType.I4)]
    [PreserveSig]
    int QueryService(ref Guid guidService, ref Guid riid, [MarshalAs(UnmanagedType.Interface)] out object ppvObject);
}
like image 94
Daniel Bogdan Avatar answered Oct 09 '22 18:10

Daniel Bogdan