Leon Barkan
Leon Barkan

Reputation: 2703

Access denied on switching to other frame (.Net WebBrowser, MsHTML) cross-domain Exception

i'm trying to switch for another frame (With no name or id) but getting Exception.

object index = 0;
var frame = (mshtml.IHTMLWindow2)workDocument.frames.item(ref index);
frameDocument = (mshtml.IHTMLDocument2)frame.document; // Exception.

Tried other way but the same exception:

webBrowser.Document.Window.Frames[0].Document.GetElementById("userName").SetAttribute("value", username);
webBrowser.Document.Window.Frames[0].Document.GetElementById("userPassword").SetAttribute("value", password);
webBrowser.Document.Window.Frames[0].Document.GetElementById("login").InvokeMember("click");

Exception:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

i understand that is a cross-domain reason, do we have solution for that cases

Thank's

Upvotes: 3

Views: 1072

Answers (1)

Sheng Jiang 蒋晟
Sheng Jiang 蒋晟

Reputation: 15271

you can catch and ignore the exception, or tweak the security settings to allow cross-domain scripting:

  • derive a class from WebBrowser
  • create a nested class derived from WebBrowser.WebBrowserSite (the only way you can derive from the nested class)
  • override CreateWebBrowserSiteBase and return a new instance of your webbrowser site.
  • implement IServiceProvider on the webbrowser site
  • implement IServiceProvider.QueryService so it returns an IInternetHostSecurityManager implementation when the SID_SInternetHostSecurityManager service is requested
  • handle IInternetHostSecurityManager.GetSecurityId and return the same domain id for all sites you want to cross-domain scripting to work with. For other web sites, give each domain a different id. Note this opens your application to cross-domain scripting attacks so you need to trust all domains sharing the same id.
  • use the new webbrowser in the form

Upvotes: 2

Related Questions