title: WebBrowser Control and the Embed Tag
body: <p>I believe I am having a security problem related to using the embed tag with a WebBrowser control in my C# 2008 WinForms application.</p>
  
  <p>Here is my code:</p>
  
  <pre><code>private void button2_Click(object sender, EventArgs e)
  {
      webBrowser1.Navigate("C:/page1.html");
  }
  
  private void button1_Click(object sender, EventArgs e)
  {
      webBrowser1.Navigate("about:blank");
      Thread.Sleep(1000);
      webBrowser1.Document.Write("&lt;html&gt;&lt;body&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;embed src='C:/test.wmv' /&gt;&lt;/body&gt;&lt;/html&gt;");
  }
  </code></pre>
  
  <p>This is the contents of page1.html:</p>
  
  <pre><code>&lt;html&gt;&lt;body&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;embed src='C:/test.wmv' /&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>Button1 generates the word "Hello".
  Button2 generates the word "Hello" with an embedded movie player below it.</p>
  
  <p>When I view the sources for both pages I notice they are identical, except for the name of the source file.</p>
  
  <p>This leads me to believe it is something to do with my IE security settings, but I notice that I have full permissions set for embedded content. Perhaps the control doesn't recognize the source of the page as proper and therefore doesn't allow the embed tag to be used.</p>
  
  <p>How can I overcome this programatically? I want to avoid writing my page to file, and navigating to that file at all cost. Any suggestions on how to trick the browser control into working properly?</p>
  
  <p><strong>1st Editor:</strong></p>
  
  <p>According to this article <A href="http://bytes.com/topic/visual-basic-net/answers/522610-webbrowser-navigate-embedded-resource" rel="noreferrer">Webbrowser Navigate Embedded Resource</a> this will work, but I (JT) tried and it didn't:</p>
  
  <pre><code>System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream("WindowsFormsApplication1.Properties.test.html");
  webBrowser1.DocumentStream = stream;
  </code></pre>
  
  <p><strong>Odd behavior while reproducing problem:</strong></p>
  
  <pre><code>webBrowser1.Navigate("about:blank");
  do
  {
  Thread.Sleep(100);
  } while (webBrowser1.IsBusy == true);
  
  //Method 1. Doesn't work
  string htmlString1 = File.ReadAllText("C:/page1.html");
  webBrowser1.Document.Write(htmlString1);
  
  //Method 2. Doesn't work
  string htmlString2 = "&lt;html&gt;&lt;body&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;embed src='C:/test.wmv' /&gt;&lt;/body&gt;&lt;/html&gt;";
  webBrowser1.Document.Write(htmlString2);
  
  //Method 3. DOES WORK
  webBrowser1.Document.Write("&lt;html&gt;&lt;body&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;embed src='C:/test.wmv' /&gt;&lt;/body&gt;&lt;/html&gt;");
  </code></pre>
  
  <p><strong>Edit 2</strong></p>
  
  <p>Here is an example of a page created with JavaScript, with no real source file, that does display the embedded player in IE:</p>
  
  <pre><code>&lt;html&gt;&lt;head&gt;
  &lt;script language="JavaScript"&gt;
  function go()
  {
  test1 = window.open("","","menubar=0,status=0,toolbar=0");
  test1.document.writeln("&lt;html&gt;&lt;body&gt;&lt;p&gt;Hello&lt;/p&gt;&lt;embed src='test.wmv' /&gt;&lt;/body&gt;&lt;/html&gt;");
  }
  &lt;/script&gt;
  &lt;/head&gt;&lt;body&gt;&lt;h1 onclick="go()"&gt;click&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>The only difference here is that IE thinks the source of the HTML is a file, although, it is created by "writeln".</p>
  
  <p>While it is popular opinion that IE does not support the  tag, it does, and there are many examples proving it. Attempting  with IE on jsfiddle.net in IE will yeild an embedded player, while in FF <em>it will not</em>.</p>
  
  <p><strong>Edit 3</strong></p>
  
  <p>This issue is related to cross-domain security. Not only do the newer versions of IE refuse to allow any changes to the domain of a page once it exists, the WebBrowser control doesn't let you write text to a document that already has text in it. Only the first call to Document.Write does anything. Not only is there no apparent way to force the domain of page, there is also no way to write anything new to a page with a domain that is set because "openNew", which is required in order to do any writing, opens about:blank and defaults to a null domain that causes exceptions if set or get is attempted.</p>
  
  <p><strong>Edit 4</strong></p>
  
  <p>The issue lies in Cross-Domain security shenanigans. See <a href="http://anton.kovalyov.net/2011/04/05/cross-domain-quirks-in-ie8/" rel="noreferrer">THIS</a> IE8 decided that Document.Domain can't be written to. Even if it were writable, you can apparently never communicate between protocols. So the "file://" protocol, and the "about" protocol can't communicate, or have  tags pointed at one another. Here are the stumbling blocks:</p>
  
  <ul>
  <li>The version of IE used by the Browser Control can't do anything to
  Document.Domain, not even with JavaScript.</li>
  <li>You can't read the domain of about:blank.</li>
  <li>You can't load a page with the proper domain, and expect to use
  Document.Write to write HTML into it, because you are forced to call
  Document.OpenNew before using Document.Write.</li>
  <li>You can't modify the DocumentText by using WebBrowser.DocumentText =
  <em>anything</em> because you can only set DocumentText once per navigation. This is like some other security <em>thing</em>.</li>
  </ul>
  
  <p>In conclusion, it is sufficient to say that you don't have any extra control over security with the WebBrowser control, likely even less than you have with some JavaScript generated pages (because these pages share the domain of the launch script).</p>
  
  <p>Thanks for the votes/support in my endeavor, but it looks like I'm going to give up and write a page to file every time I want to change what is in the browser control. Yuck.</p>