Archive | Silverlight RSS for this section

Wpf WebBrowser control and silverlight IE security issue

Its long since I worked with WPF and silverlight :(. But for the last one week I have been playing around with both of them. The case scenario that I was trying to implement was a simple intercommunication between wpf and silverlight. The requirement was very simple, as I need to build a WPF user control that wraps a simple silverlight control which is hosted in WebBrowser control with a two way communication between the WPF client and the silverlight implementation via javascript.

I will briefly describe the implementation details so as to know how I got to the problem.

First, I created a silverlight application which was supposed to  have a edit box and a button. On the html page hosting the silverlight, I added javascript  functions that were supposed to set and get the text value of the textbox on the silverlight. Also I added a handler that will be handle  silverlight button click event

And just for testing my javascript, I added some two buttons on the html file and which worked which worked great.

Then, I created a WPF user control, which utilized WebBrowser control to host the silverlight xap file. And just to do it the hard way, I dynamically wrote the html for the WebBrowser control including the javascript functions and the silverlight button handler just like I did on the html page.

And to consume my user control, I created a simple WPF client, which had three controls on its main page and handler to be used when the silverlight button is clicked.

-My custom control

-Button to set the edit box on silverlight

-Button to get the value of the silverlight edit box.

And upto here everything worked fine. For more details on how to set up all this go to here

Having done all this, I expected that my when I run my WPF client, I will see my silverlight control, but oops, I got this screen 😦

silverlight

This is a security alert informing that the content contain some ActiveX components. WebBrowser  will display a warning everytime you load the component. This took me to a thorough research on the web and found various articles pointing out that the solution was to add the mark of the web. Unfortunately, this did not work out  for me.

After researching and consulting, I ended up to this function  “CoInternetSetFeatureEnabled” which you can find more details here. And here was my quick implementation which I added to the user control which is basically some simple pinvoke. By looking at the function, you will note that it takes three parameters,

Feature entry: This is a value from  InternetFeatureList enumeration showing which feature to enable or disable. And so I added this enumeration:

internal enum INTERNETFEATURELIST
{
FEATURE_OBJECT_CACHING = 0,
FEATURE_ZONE_ELEVATION = 1,
FEATURE_MIME_HANDLING = 2,
FEATURE_MIME_SNIFFING = 3,
FEATURE_WINDOW_RESTRICTIONS = 4,
FEATURE_WEBOC_POPUPMANAGEMENT = 5,
FEATURE_BEHAVIORS = 6,
FEATURE_DISABLE_MK_PROTOCOL = 7,
FEATURE_LOCALMACHINE_LOCKDOWN = 8,
FEATURE_SECURITYBAND = 9,
FEATURE_RESTRICT_ACTIVEXINSTALL = 10,
FEATURE_VALIDATE_NAVIGATE_URL = 11,
FEATURE_RESTRICT_FILEDOWNLOAD = 12,
FEATURE_ADDON_MANAGEMENT = 13,
FEATURE_PROTOCOL_LOCKDOWN = 14,
FEATURE_HTTP_USERNAME_PASSWORD_DISABLE = 15,
FEATURE_SAFE_BINDTOOBJECT = 16,
FEATURE_UNC_SAVEDFILECHECK = 17,
FEATURE_GET_URL_DOM_FILEPATH_UNENCODED = 18,
FEATURE_ENTRY_COUNT = 19,
};

dwFlags: This specifies at what level do you want to put the feature control value. To my case I wanted it on the current process And so I declared the following constants

internal const uint SET_FEATURE_ON_THREAD = 0x00000001;
internal const uint SET_FEATURE_ON_PROCESS = 0x00000002;
internal const uint SET_FEATURE_IN_REGISTRY = 0x00000004;
internal const uint SET_FEATURE_ON_THREAD_LOCALMACHINE = 0x00000008;
internal const uint SET_FEATURE_ON_THREAD_INTRANET = 0x00000010;
internal const uint SET_FEATURE_ON_THREAD_TRUSTED = 0x00000020;
internal const uint SET_FEATURE_ON_THREAD_INTERNET = 0x00000040;
internal const uint SET_FEATURE_ON_THREAD_RESTRICTED = 0x00000080;

fEnable: And this is simply to indicate the feature should be enable or not. And so I did not want enable, since I wanted it for the current process, and the value was false.

With that in place I declared the function as below,

[DllImport("urlmon.dll")]
[return: MarshalAs(UnmanagedType.Error)]
internal static extern int CoInternetSetFeatureEnabled(uint FeatureEntry, [MarshalAs(UnmanagedType.U4)] uint dwFlags, bool fEnable);

In my ctor I called the function as below, passing the three arguements as per my scenario.

CoInternetSetFeatureEnabled((uint)INTERNETFEATURELIST.FEATURE_LOCALMACHINE_LOCKDOWN, SET_FEATURE_ON_PROCESS, false);

Hope this helps someone who might have the same case!

The best way to know you were here is to leave a comment, suggestions for topics you would like covered or even corrections.

Happy coding and see you here for another great post!

Oh Wait,please take a second to like our facebook page here. Thanks!!   🙂

%d bloggers like this: