Iframe Access denied error

Add-in Express™ Support Service
That's what is more important than anything else

Iframe Access denied error
 
ryan J




Posts: 5
Joined: 2011-08-03
When I try to walk through the DOM of the page in the Iframe where the src is different domain, I get mshtml.IHTMLFrameBase {System.UnauthorizedAccessException}. When I try using the VS debugger to step through the object tree, when I get to the iframe on this domain, all the values say "Access Denied" or something similar. Is there any way to access iFrame DOM which has src of different domain.
Posted 03 Aug, 2011 22:51:11 Top
ryan J




Posts: 5
Joined: 2011-08-03
Can anybody please answer this for me.
Posted 04 Aug, 2011 06:15:57 Top
ryan J




Posts: 5
Joined: 2011-08-03
When I try to walk through the DOM of the page in the Iframe where the src is different domain, I get mshtml.IHTMLFrameBase {System.UnauthorizedAccessException}. When I try using the VS debugger to step through the object tree, when I get to the iframe on this domain, all the values say "Access Denied" or something similar. Is there any way to access iFrame DOM which has src of different domain.
Posted 04 Aug, 2011 08:51:02 Top
Vladimir Lvov




Posts: 10
Joined: 2010-09-16
This function will do the conversion without UnauthorizedAccessException.

public static HTMLDocument ConvertToDocument(IHTMLElement frame)
        {
            try
            {
                return (HTMLDocument)((Interop.SHDocVw.IWebBrowser2)((HTMLFrameElement)frame)).Document;
            }
            catch
            {
                return null;
            }
        }
Posted 04 Aug, 2011 10:07:21 Top
ryan J




Posts: 5
Joined: 2011-08-03
Thanks a lot.
I tried your solution and getting following error.

Message: Unable to cast COM object of type 'mshtml.HTMLWindow2Class' to interface type 'mshtml.IHTMLFrameElement'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{3050F313-98B5-11CF-BB82-00AA00BDCE0B}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

My Code in VB.NET:
Private Sub __doHighlight(ByVal searchTerm As String, highlightColor As String)
Dim document As mshtml.HTMLDocument = HTMLDocument
If hasFrames() Then
Dim k As Integer = 0
For k = 0 To document.frames.length - 1
Dim frHTMLDoc As HtmlDocument = ConvertToDocument(document.frames.item(k))
If Not frHTMLDoc.Body Is Nothing Then
Dim bodyElement As Object = frHTMLDoc.Body
__highlightText(bodyElement, searchTerm, highlightColor)
bodyElement = Nothing
End If
Next
Else
If Not document.body Is Nothing Then
Dim bodyElement As Object = document.body
__highlightText(bodyElement, searchTerm, highlightColor)
bodyElement = Nothing
End If
End If
End Sub

Public Function ConvertToDocument(frame As mshtml.IHTMLFrameElement) As HtmlDocument
Try
Return DirectCast(DirectCast(DirectCast(frame, mshtml.HTMLFrameElement), Interop.SHDocVw.IWebBrowser2).Document, HtmlDocument)
Catch
Return Nothing
End Try
End Function
Posted 04 Aug, 2011 11:24:04 Top
Vladimir Lvov




Posts: 10
Joined: 2010-09-16
Select frames as html elements instead of using document.frames.


             string[] frameTags = new string[] { "FRAME", "IFRAME" };
            foreach (string frameTag in frameTags)
            {
                IHTMLElementCollection framesCollection = document.getElementsByTagName(frameTag);
                foreach (IHTMLElement frame in framesCollection)
                {
                    HTMLDocument childDoc = ConvertToDocument(frame);
                    // Put your logic here
                }
            }
Posted 04 Aug, 2011 11:52:48 Top
ryan J




Posts: 5
Joined: 2011-08-03
Thanks a lot. Its working fine now.
Posted 04 Aug, 2011 12:18:27 Top
Sergey Grischenko


Add-in Express team


Posts: 7233
Joined: 2004-07-05
Hi Vladimir,

Thank you for the help. I have just sent you an email. Please check your inbox.
Posted 05 Aug, 2011 06:38:00 Top