2012-02-20 87 views
0

ETA:我已經有了它,答案在下面發佈。我會標記任何人可以解釋到底發生了什麼以及如何清理它的答案。winforms網頁瀏覽器控件中的JavaScript訪問問題

我有一個類,其中包含一個窗體與WinForms的瀏覽器控件來顯示一個HTML頁面。我在頁面的頭部插入了一些腳本,以便查詢有關樣式的信息。

當我從可執行文件啓動應用程序時,一切正常。不過,我現在試圖在設計時從外接程序啓動應用程序。會發生什麼情況是右鍵單擊.htm類型文件並單擊以啓動瀏覽器。然後加載項啓動導航到提供的.htm文件路徑的瀏覽器。

我注意到的第一件事是,瀏覽器現在顯示此消息:

爲幫助保護您的安全,您的瀏覽器已經限制顯示可能訪問您的計算機的活動內容文件點擊這裏。選項...

然後我注意到,即使點擊啓用活動內容,我的所有javascript調用都失敗了。

作爲一個測試,我想這簡單的JavaScript調用(沒有明確注入):

Me.Document.InvokeScript("execScript", New Object() {"alert('hello');", "JavaScript"}) 

這將導致一個JavaScript錯誤對話框,說:「訪問被拒絕

所以,這是一個安全問題。不是我想要但是,我試圖暫時降低IE中的所有安全級別,但這沒有什麼區別。

我應該補充說,該應用程序包含2個瀏覽器控件。第二個託管通過設置瀏覽器的屬性創建的網頁。這不會遭受javascript訪問問題。

ETA:我一直在尋找IInternetSecurityManager,這可能與此有關嗎?我希望不會:(

回答

0

我設法得到它與IInternetSecurityManager的工作是由WebBrowser控件的網站返回的服務。 我得到了它在URL中ProcessUrlAction方法返回好吧,無論工作。

我從我在互聯網上找到位這個那兒剽竊在一起,如果有人能指出它是如何進行清理,限制在企業內部網的話,我會慶祝,作爲答案。

我相信我需要在ProcessUrlAction中檢查url,並根據其內容返回Ok或Default。

下面的代碼:

Friend Class MainBrowser 
Inherits WebBrowser 

Private _Site As WebBrowserSite 
Protected Overrides Function CreateWebBrowserSiteBase() As WebBrowserSiteBase 
    If _Site Is Nothing Then 
     _Site = New WebBrowserSite(Me) 
    End If 
    Return _Site 
End Function 

Protected Class WebBrowserSite 
    Inherits System.Windows.Forms.WebBrowser.WebBrowserSite 
    Implements NativeInterfaces.IServiceProvider 
    Implements NativeInterfaces.IInternetSecurityManager 

    Private Const INET_E_DEFAULT_ACTION As Integer = &H800C0011 
    Private Const S_OK As Integer = 0 
    Private Const E_NOINTERFACEX As Integer = &H80004002 

    Private Shared IID_IInternetSecurityManager As Guid = Marshal.GenerateGuidForType(GetType(NativeInterfaces.IInternetSecurityManager)) 

    Private Owner As MainBrowser 

    Public Sub New(ByVal owner As MainBrowser) 
     MyBase.New(owner) 
     owner = owner 
    End Sub 

    Public Function QueryService(ByRef guidService As System.Guid, ByRef riid As System.Guid, ByRef ppvObject As System.IntPtr) As Integer Implements NativeInterfaces.IServiceProvider.QueryService 
     If guidService = IID_IInternetSecurityManager AndAlso riid = IID_IInternetSecurityManager Then 
      ppvObject = Marshal.GetComInterfaceForObject(Me, GetType(NativeInterfaces.IInternetSecurityManager)) 
      Return S_OK 
     End If 
     ppvObject = IntPtr.Zero 
     Return E_NOINTERFACEX 
    End Function 

    Public Function GetSecurityId(ByVal pwszUrl As String, ByVal pbSecurityId As System.IntPtr, ByRef pcbSecurityId As UInteger, ByRef dwReserved As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.GetSecurityId 
     Return INET_E_DEFAULT_ACTION 
    End Function 

    Public Function GetSecuritySite(ByRef pSite As System.IntPtr) As Integer Implements NativeInterfaces.IInternetSecurityManager.GetSecuritySite 
     pSite = IntPtr.Zero 
     Return INET_E_DEFAULT_ACTION 
    End Function 

    Public Function SetSecuritySite(ByVal pSite As System.IntPtr) As Integer Implements NativeInterfaces.IInternetSecurityManager.SetSecuritySite 
     Return INET_E_DEFAULT_ACTION 
    End Function 

    Public Function MapUrlToZone(ByVal pwszUrl As String, ByRef pdwZone As UInteger, ByVal dwFlags As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.MapUrlToZone 
     pdwZone = 0 // URLZONE_LOCAL_MACHINE ? 
     Return S_OK // no difference 
     // Return INET_E_DEFAULT_ACTION 
    End Function 

    Public Function ProcessUrlAction(ByVal pwszUrl As String, ByVal dwAction As UInteger, ByVal pPolicy As System.IntPtr, ByVal cbPolicy As UInteger, ByVal pContext As System.IntPtr, ByVal cbContext As UInteger, ByVal dwFlags As UInteger, ByVal dwReserved As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.ProcessUrlAction 
     // Return INET_E_DEFAULT_ACTION 
     Return S_OK // This is what made the difference 
    End Function 

    Public Function QueryCustomPolicy(ByVal pwszUrl As String, ByRef guidKey As System.Guid, ByRef ppPolicy As System.IntPtr, ByRef pcbPolicy As UInteger, ByVal pContext As System.IntPtr, ByVal cbContext As UInteger, ByVal dwReserved As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.QueryCustomPolicy 
     ppPolicy = IntPtr.Zero 
     pcbPolicy = 0 
     Return INET_E_DEFAULT_ACTION 
    End Function 

    Public Function SetZoneMapping1(ByVal dwZone As UInteger, ByVal lpszPattern As String, ByVal dwFlags As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.SetZoneMapping 
     Return INET_E_DEFAULT_ACTION 
    End Function 

    Public Function GetZoneMappings(ByVal dwZone As UInteger, ByRef ppenumString As System.Runtime.InteropServices.ComTypes.IEnumString, ByVal dwFlags As UInteger) As Integer Implements NativeInterfaces.IInternetSecurityManager.GetZoneMappings 
     ppenumString = Nothing 
     Return INET_E_DEFAULT_ACTION 
    End Function 

End Class 

End Class 

的接口:

<ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), Guid("6d5140c1-7436-11ce-8034-00aa006009fa")> _ 
Interface IServiceProvider 
    <PreserveSig()> _ 
    Function QueryService(ByRef guidService As Guid, ByRef riid As Guid, ByRef ppvObject As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer 
End Interface 


<ComImport(), GuidAttribute("79EAC9EE-BAF9-11CE-8C82-00AA004BA90B"), InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _ 
Public Interface IInternetSecurityManager 
    <PreserveSig()> _ 
    Function SetSecuritySite(<[In]()> ByVal pSite As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer 

    <PreserveSig()> _ 
    Function GetSecuritySite(ByRef pSite As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer 

    <PreserveSig()> _ 
    Function MapUrlToZone(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, ByRef pdwZone As UInt32, <[In]()> ByVal dwFlags As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer 

    <PreserveSig()> _ 
    Function GetSecurityId(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, <Out()> ByVal pbSecurityId As IntPtr, <[In](), Out()> ByRef pcbSecurityId As UInt32, <[In]()> ByRef dwReserved As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer 

    <PreserveSig()> _ 
    Function ProcessUrlAction(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, ByVal dwAction As UInt32, ByVal pPolicy As IntPtr, ByVal cbPolicy As UInt32, ByVal pContext As IntPtr, ByVal cbContext As UInt32, _ 
     ByVal dwFlags As UInt32, ByVal dwReserved As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer 

    <PreserveSig()> _ 
    Function QueryCustomPolicy(<[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal pwszUrl As String, ByRef guidKey As Guid, ByRef ppPolicy As IntPtr, ByRef pcbPolicy As UInt32, ByVal pContext As IntPtr, ByVal cbContext As UInt32, _ 
     ByVal dwReserved As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer 

    <PreserveSig()> _ 
    Function SetZoneMapping(ByVal dwZone As UInt32, <[In](), MarshalAs(UnmanagedType.LPWStr)> ByVal lpszPattern As String, ByVal dwFlags As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer 

    <PreserveSig()> _ 
    Function GetZoneMappings(<[In]()> ByVal dwZone As UInt32, ByRef ppenumString As ComTypes.IEnumString, <[In]()> ByVal dwFlags As UInt32) As <MarshalAs(UnmanagedType.I4)> Integer 
End Interface