C# WebBrowser Control Enhanced Protected Mode - c#

I am trying to access a webpage (that is not under my control) namely allscripts sandbox via a WebBrowser control. My computer's internet explorer is correctly set up for said webpage (Added in Trusted Sites, Allowed and installed all active-x addons, running in compatibility mode, etc).
The webbrowser control displays the following error:
This webpage wants to run 'Some ActiveX control' which isn't compatible with Internet Explorer's enhanced security features. If you trust this site you can disable Enchanced Protected Mode for this site and allow the control to run.
I have not enabled (to the best of my knowledge) the enhanced protected mode.
Also trying to ignore the errors and continue with log-in displays a Message
The Centricity's container for .NET-based pages failed to initialize. Make sure your .NET environment is configured to grant Full Trust to this Website.
The above was also an error on the default IE until i run the command %WINDIR%\Microsoft.NET\Framework\v2.0.50727\caspol -q -m -cg Trusted_Zone FullTrust.
I have tried various registry keys but none seemed to work.
I have also tried implementing a custom IInternetSecurityManager that Maps all urls to zone Trusted and returns URLPOLICY_ALLOW on all ProcessUrlAction calls.
Any suggestion would be appreciated.

The problem could be that webbrowser uses by default an old version of the IE. Take a look at Use latest version of Internet Explorer in the webbrowser control

The webbrowser control is ie11 wrapped with a com wrapper that throttles back ie11 to ie7 mode. There's not a lot else going on there that I can imagine would cause your issue.
Since this page works for you when you run ie11 externally then the most likely explanation seems to be your attempt to force the control into ie11 mode is the problem.
I suggest you try Mentor's code here:
Set WPF webbrowser control to use IE10 mode
Which will automate adding the name of the running program to the registry.
var pricipal = new System.Security.Principal.WindowsPrincipal(
System.Security.Principal.WindowsIdentity.GetCurrent());
if(pricipal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)) {
RegistryKey registrybrowser = Registry.LocalMachine.OpenSubKey
(#"Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", true);
string myProgramName = Path.GetFileName(System.Reflection.Assembly.GetExecutingAssembly().Location);
var currentValue = registrybrowser.GetValue(myProgramName);
if (currentValue == null || (int)currentValue != 0x00002af9)
registrybrowser.SetValue(myProgramName, 0x00002af9, RegistryValueKind.DWord);
}
else
this.Title += " ( Первый раз запускать с правами админа )";

Related

IE's zone elevation protection interferences functionality of BHO

I am developing a Browser Helper Object (BHO) for Internet Explorer written in C#. I use the BeforeNavigate event to get a called URL and save it into a local variable. For every tab a new BHO instance is spawned. This means that every tab has it's own BHO which in turn have own local variables. I have checked this by displaying a MessageBox with the previous called URL (the value of the local variable) before it is overwritten with the new URL.
string myUrl = "";
void BeforeNavigate( string URL, ... )
{
System.Windows.Forms.MessageBox.Show( myUrl );
myUrl = URL.ToString();
}
But in some cases the local variable is empty although a URL was called before. I investigated the IE settings and found out that this behavior is caused by the zone elevation protection of IE. For the zones local intranet and trusted sites the protected mode is disabled while it is enabled for zones internet and restricted sites.
E.g., when intranet.com is called and then internet.com in the same tab, I would expect that the MessageBox displays intranet.com when internet.com is called. But an empty string is displayed instead. I guess that calling internet.com activates the protected mode for this tab which spawns a new instance of the BHO. The MessageBox will now display the value of the variable of the new BHO instance. The value of the variable of the old BHO gets lost.
If protected mode is enabled for zones local intranet and trusted sites the BHO behaves correctly. I guess that the protected mode is disabled in this zones for compatibility reasons. There may exists websites in the intranet that do not work with protected mode. Thus, I am looking for a solution that works with protected mode disabled for this zones.
Since IE manages the loading of the BHO I doubt that this problem can be solved from within the BHO.
Does anybody have deeper knowledge about this topic to confirm my guess?
Is it possible to keep the variable's value with protected mode disabled for zones local intranet and trusted sites?
Any help will be appreciated, thanks!
I found the following link: http://jimevansmusic.blogspot.com/2012/08/youre-doing-it-wrong-protected-mode-and.html
There is said:
When you cross into or out of Protected Mode by, say, navigating from an internal intranet website to one on the internet, IE has to create a new process, because it can't change the Mandatory Integrity Control level of the existing process. Moreover, in IE versions after 7, it's not always obvious that a Protected Mode boundary has been crossed, since IE tries to present a better user experience by seamlessly merging the browser window of the new process with the already opened browser window. This under-the-covers process switching also means that any references pointing to IE's COM objects before the Protected Mode boundary crossing are left pointing to objects that are no longer used by IE after the boundary crossing.
Based on this, my guess seems to be right. The BHO which is a COM object of IE is no longer used and thus it's value gets lost. The only solution that remains is to enable or disable protected mode for all zones.

WebBrowser control throwing lots of JavaScript errors

The WebBrowser control shipped with .NET 4.7 is throwing lots of JavaScript errors. For example, we tried with www.yahoo.com and we get multiple JavaScript errors. One such example is shown below:
If we browse the same link in internet explorer 11, we are not getting any error and browsing session goes smooth. Note that we already set Browser Emulation registry setting for IE 11. Used the Link to Set IE 11 Emulation. The Registry Key for the Emulation is:
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION
(For 64 bit machine)
Re-production steps
Create a Visual C# Windows Forms Application
Place Web-Browser Control and a Command Button in the Form
Add the Following code in the command button Handler:
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.yahoo.com");
}
That is all. Once we click the command button, we have to respond to multiple JavaScript errors. How do we resolve this issue?
Note that we already have Browser Emulation for IE is set to 11 (in Registry). We do not want to suppress the Error, as we lose the service provided by the JavaScript. For example, click the “Cricket” button in the very top of the Yahoo page:

Unable to click login button on angular site in IE (Selenium)

For some reason I seem unable to click a login button when using the selenium IEDriver. It works fine in both Chrome and Firefox.
Website is http://www.notacv.com/
Simple code...
IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.notacv.com");
IWebElement element2 = driver.FindElement(By.CssSelector("div.container-fluid.hero ul li:nth-child(1)"));
element2.Click();
I've tried the following to remedy the problem with no success
Have tried referencing both the li and the a tags
Both XPath and CSS selector - both confirm to find only one element via Chrome developer toolbar
Thread.sleep (prevents the error but does nothing when it gets to the click)
Repeating the click and the wait
Javascript, JQuery and angular waits
SendKeys(Keys.Enter)
actions -> moveto.Click()
Explicit wait to check displayed, visibility, enabled
Protractor for C#
Does anyone know how to fix this for IE?
please check the current link https://github.com/SeleniumHQ/selenium/wiki/InternetExplorerDriver#Required_Configuration
Make sure you applied required changes
The IEDriverServer exectuable must be downloaded and placed in your PATH.
On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value
can be on or off, as long as it is the same for every zone. To set
the Protected Mode settings, choose "Internet Options..." from the
Tools menu, and click on the Security tab. For each zone, there will
be a check box at the bottom of the tab labeled "Enable Protected
Mode".
Additionally, "Enhanced Protected Mode" must be disabled for IE 10 and higher. This option is found in the Advanced tab of the Internet
Options dialog.
The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.
For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance
of Internet Explorer it creates. For 32-bit Windows installations,
the key you must examine in the registry editor is
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet
Explorer\Main\FeatureControl\FEATURE_BFCACHE. For 64-bit Windows
installations, the key is
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet
Explorer\Main\FeatureControl\FEATURE_BFCACHE. Please note that the
FEATURE_BFCACHE subkey may or may not be present, and should be
created if it is not present. Important: Inside this key, create a
DWORD value named iexplore.exe with the value of 0.

Local load of HTML/Javascript file in C# WebBrowser object not loading correctly?

I'm using .NET Framework 4.0 (with WPF) trying to load a local HTML file within a WebBrowser object (System.Windows.Controls.WebBrowser) with both locally embedded javascript and loaded from a remote server. The problem is, the javascript (ajax with dojo) isn't executing inside the WebBrowser object when loaded:
webBrowser.NavigateToString(LoadStringFromFile("map.html"));
However, when loaded remotely it runs just fine as follows:
webBrowser.Navigate("http://www.example.com/map.html");
Sample excerpt code of Javascript code in html file:
<script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=1.6"></script>
<script type="text/javascript">
dojo.require("esri.map");
var map;
var colorRGB = { "white": [255,255,255], "red": [255,0,0], "blue": [0,255,0] };
function init()
{
var streetLayer = new esri.layers.ArcGISTiledMapServiceLayer(
"http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer");
var extent = new esri.geometry.Extent(-140.910, 11.267, -53.019, 64.002);
map = new esri.Map("map", { extent:extent });
map.addLayer(streetLayer);
dojo.connect(map,"onLoad", processLocations);
}
Anyone have any idea what is wrong with this?
My research suggest it has to do with IE and internet zones, but have been unable to confirm it.
When i paste this into an html file and open on internet explorer, it display a message that it blocked activex/script content. Change Internet Options > Advanced > Security > Allow active content to run in files on My Computer. If the warning goes away in IE, then it should work in WebBrowser object
The WPF WebBrowser class has a static initializer that enables FEATURE_LOCALMACHINE_LOCKDOWN for the entire process. You can use CoInternetSetFeatureEnabled to turn FEATURE_LOCALMACHINE_LOCKDOWN back off on the WebBrowser Navigating event.
This solution does not require you to modify the HTML files being displayed, but does disable some security features that you probably don't want when displaying trusted local content. See http://technet.microsoft.com/en-us/library/cc782928(v=ws.10).aspx for more information about local machine lockdown.
See How to disable click sound in WebBrowser Control for PInvoke and https://github.com/TaoK/PoorMansTSqlFormatter/blob/d6b4f7bedc02ce1bf59acb16dd1f49609c216aa7/PoorMansTSqlFormatterDemo/FrameworkClassReplacements/CustomContentWebBrowser.cs for an example usage.
Here is my solution for preventing scriptwarnings:
WebBrowser.ScriptErrorsSuppressed = true;
Also I've put my website as a trusted website in my Internet Settings, I don't know if that helps but the scriptErrorsSuppressed property was enough for me.
Hope this helps.
For me, the problem was:
Steps to enable ActiveX controls in Internet Explorer:
1.Select Tools --> Internet Options menu from the Internet Explorer.
2.Select the Security tab from the Internet Options dialog.
3.Select the appropriate Web content zone and click Custom Level.
4.Make the following options available under ActiveX controls and plug-ins to either enableor Prompt:1.Download signed ActiveX controls
2.Run ActiveX controls and plug-ins
3.Script ActiveX controls marked safe for scripting
5.Click Ok to save the security settings.
6.Click OK to save and close the Internet Options dialog.
Then click don't show this message again.

WebBrowser control and JavaScript errors

When I access the page with the browser (ie9), the browser is rendering ok.
When I use the WebBrowser control I have JavaScript errors.
I know I can suppress the scripts errors, but I want them to run correctly, because they affect the rendering and the functionality of the page.
How can I solve this problem ? Can I integrate IE9 directly in the Windows Form and use similar methods like with the WebBrowser control (navigate,get id, invoke click) ?
Thanks.
What I would do is assign an object to webbrowser.ObjectForScripting and then inject a javascript function that assigns windown.onerror to a wrapper that calls the external script in the host app. Like:
window.onerror = function(message, url, lineNumber)
{
window.external.errorHandler(message, url, lineNumber);
}
Refere to:
http://notions.okuda.ca/2009/06/11/calling-javascript-in-a-webbrowser-control-from-c/
If you have IE9 installed, the WebBrowser will still use IE7 mode unless you override this behaviour with a registry setting - as described in this StackOverflow answer. This is the most likely cause of the JavaScript errors you're getting in the WebBrowser (because you're not seeing the same errors in IE9).
You can make the registry setting using the following c# code (which sets IE10 mode if Windows 8 is detected) and changing app-name.exe to match your own application. You should add an error handler for the case where there are insufficient privileges (admin privileges are required to write to this registry key).
string installkey = #"SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION";
string entryLabel = "app-name.exe";
System.OperatingSystem osInfo = System.Environment.OSVersion;
string version = osInfo.Version.Major.ToString() + '.' + osInfo.Version.Minor.ToString();
uint editFlag = (uint)((version == "6.2") ? 0x2710 : 0x2328); // 6.2 = Windows 8 and therefore IE10
RegistryKey existingSubKey = Registry.LocalMachine.OpenSubKey(installkey, false); // readonly key
if (existingSubKey.GetValue(entryLabel) == null)
{
existingSubKey = Registry.LocalMachine.OpenSubKey(installkey, true); // writable key
existingSubKey.SetValue(entryLabel, unchecked((int)editFlag), RegistryValueKind.DWord);
}
You can use the following code line to get rid of those types of errors:
webBrowser1.ScriptErrorsSuppressed = true;
It will prevent getting JavaScript errors.
So i know the post is old, but it was a recent problem for me and i had to do some serious digging and thinking outside the box.
basically like most replies here - you cannot alter the webbrowser control to use the most recent IE engine. mine uses IE7 by default, i have seen some replies that basically changes/ adds stuff to registry, am always not comfy when it comes to the registry, a cleaner way to address this issue would be to append code on your website that forces it to use the most current IE engine on any pc and works like a charm.
if you have access to the web.config file of the page you intend to browse, simple append:
<system.webServer>
<httpProtocol>
<customHeaders>
<clear />
<add name="X-UA-Compatible" value="IE=edge" />
</customHeaders>
</httpProtocol>
</system.webServer>
and your site would force webbrowser control to run the most current IE engine on your computer. Other options are found here:
https://www.leapinggorilla.com/Blog/Read/1016/ie-ate-my-css---disabling-compatability-mode
I should state that this would only work if you have access to the page / web.config of the website/ application you are trying to access- which was my case.
THe WebBrowser control uses IE7. So if there is a problem then your script does not work for IE7 and you will have to fix that.
You cannot integrate IE9 as it depends on it being installed on the computer and not everyone has IE9 installed.
As a help to whoever else may have this problem, I tried all these things and nothing worked for me. Here's what does work. I am not sure exactly what causes this error, but apparently when you just press "F5" in VS to debug your app, it runs YourProject.vshost.exe as the process name. If you run the same app from the command line, it will show up as YourProject.exe, and the javascript errors vanish. I think IE sees the app running visa via VSHOST and decides this is fishy and disables javascript from loading correctly.
So... go into your project setting for your executable.
Select "Debug" options.
Select "Start External Program".
Browse to and select Debug\YourProgram.exe (NOT YourProgram.vshost.exe).
Save, recompile, and hit F5.
Everything should work as per usual now, and Visual Studio even attaches to the process for you automatically.
Enjoy!
Grego

Categories

Resources