【问题标题】:WebBrowserControl: UnauthorizedAccessException when accessing property of a FrameWebBrowserControl:访问 Frame 的属性时出现 UnauthorizedAccessException
【发布时间】:2018-01-15 06:39:42
【问题描述】:

我使用默认的 WebBrowser 控件在 C# 中编写了一个非常小的网站机器人。实际上,几乎所有东西都按照它应该的方式工作,但我似乎在自动化的最后一步遇到了问题。

该网站是使用多个 iframe 构建的。这没什么大不了的,因为我只是使用

访问这些框架及其元素
webBrowser1.Document.Window.Frames[0].Document.GetElementById("element").InvokeMember("click");

但是,当 IFRAME 的源托管在与实际网站不同的域上时,这不起作用。当我在互联网上搜索我的问题的答案时,我偶然发现了一篇 MSDN 文章提到了这个特定问题,他们指的是针对跨站点脚本的安全措施,这可能是导致此错误的原因。

我真的找不到禁用此功能的方法,所以我继续并决定重新编码所有内容以使用 geckofx-12 而不是默认(基于 IE)的 Web 浏览器控件,但我遇到了类似的问题.. .

我的问题是:有什么办法可以绕过这种烦人的行为吗?我并不真正关心安全问题,也不关心是否正在使用 geckofx 或默认 Web 浏览器控件,我只想以编程方式访问托管在不同域上的站点的元素没有 遇到 UnauthorizedAccessException。

我很想从那里的大师那里得到建议。

【问题讨论】:

    标签: c# .net vb.net


    【解决方案1】:

    您无法访问来自不同域的框架。这是一个安全功能。有一个小技巧:

     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);
    }
    

    【讨论】:

    • 什么是 IWebBrowser2
    • 如果我能多次投票给你,我会这么做的。完美运行。
    • @Kiquenet 显然来自 SHDocVw:stackoverflow.com/questions/20845140/…
    • 我添加了对 dll 的引用,但 SHDocVw 命名空间中仍然没有 IHTMLDocument2
    【解决方案2】:

    我稍微更新了 Daniel Bogdan 发布的 hack 以使用扩展方法,并为您提供了一种无需进入 mshtml 命名空间即可调用它的方法:

    using mshtml;
    using SHDocVw;
    using System;
    using System.Reflection;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;
    
    namespace TradeAutomation
    {
        public static class CrossFrameIE
        {
            private static FieldInfo ShimManager = typeof(HtmlWindow).GetField("shimManager", BindingFlags.NonPublic | BindingFlags.Instance);
            private static ConstructorInfo HtmlDocumentCtor = typeof(HtmlDocument).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
    
            public static HtmlDocument GetDocument(this HtmlWindow window)
            {
                var rawDocument = (window.DomWindow as IHTMLWindow2).GetDocumentFromWindow();
    
                var shimManager = ShimManager.GetValue(window);
    
                var htmlDocument = HtmlDocumentCtor
                    .Invoke(new[] { shimManager, rawDocument }) as HtmlDocument;
    
                return htmlDocument;
            }
    
    
            // Returns null in case of failure.
            public static IHTMLDocument2 GetDocumentFromWindow(this 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);
        }
    }
    

    用法:

    webBrowser1.Document.Window.Frames["main"].GetDocument();
    

    正如我在上面的评论中提到的,您还需要添加对 SHDocVw 的引用。你可以在这里找到方向:Add reference 'SHDocVw' in C# project using Visual C# 2010 Express

    【讨论】:

      【解决方案3】:

      我还没有尝试过,但 changing the document domain 显然有效。

      使用 geckofx 12 看起来这可能由 nsIDOMHTMLDocument.SetDomainAttribute 完成(GeckoDocument.Domain 没有设置器,但您可以轻松添加它)

      IE。如果您更改文档的域以匹配子框架,您也许可以访问它。

      【讨论】:

      • 不幸的是,我在尝试使用此方法时遇到了 COM 异常。然而,我通过使用 webBrowser1.Document.GetElementsByTagName("iframe") 获取 iframe 并通过 ((Gecko.DOM.GeckoIFrameElement)frames[0]).ContentDocument 访问它们的内容文档来让事情正常工作。不过,我将您的答案标记为解决方案,因为无论如何都没有其他答案。
      猜你喜欢
      • 1970-01-01
      • 2012-02-17
      • 1970-01-01
      • 2018-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-08
      • 1970-01-01
      相关资源
      最近更新 更多