Monday, May 16, 2011

Silverlight 5 Beta - Run Elevated Trust Applications in Browser

In this article, we will see how to run an Elevated Trust Application within the Browser using Silverlight 5 Beta.  For this demonstration, we will first create a Silverlight application by using Visual Studio 2010 with Silverlight Version 5.  Read my article Silverlight 5 Beta: What’s New? if you are new to Silverlight 5.

We will rename the application - 'TrustedAppsInBrowser' as shown below -
Make sure you choose Silverlight version 5.0. Now let's add a reference of a API 'Microsoft.CSharp.dll' to our Silverlight application. Browse this API from the path - ' C:\Program Files (x86)\Microsoft SDKs\Silverlight\v4.0\Libraries\Client'.

Design the MainPage.xaml. Replace <Grid></Grid> with following code -
<Grid x:Name="LayoutRoot" Background="Black">         
    <Grid.RowDefinitions>  
...
...
Now let's import a namespace to our code behind -
using System.Runtime.InteropServices.Automation;

image.png

Now run your application and click on the 'Send' button. You will get an exception as shown below -

clip_image0021.jpg

The operation is not supported in this context. You cannot call COM components directly from the browser due to security reasons. However you can achieve the same by making this Silverlight Application as an Out-Of-Browser application with Elevated Trust. I have already demonstrated this in my previous article 'Silverlight 4 and COM - Sending Mails using Outlook'. If you have not read it, I will highly recommend you to go through it first.

However we have to run this application in the Browser. To run this application in the browser, you must set this application to run Out-Of-Browser with Elevated Trust. Right click the Silverlight Application and go to Properties. Choose checkbox 'Enable running application Out of the browser' and click on 'Out of browser settings' button. From the setting window, chose a checkbox 'Require Elevated trust when running outside the browser'.

Now you will have to follow the steps given below for enabling our Silverlight application to run within the browser, with Elevated trust.
Step 1: Go to Start and click on Run.  Type 'RegEdit'. This will open the 'Registry Editor' window. Now the most important thing is that If you running the app on a 32 bit machine, you will have to find -

· HKEY_LOCAL_MACHINE\Software\Microsoft\Silverlight\

and if you are running on a 64-bit machine, you will have to find -

· HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Silverlight\

I took the reference of MSDN for this path. My computer is 64-bit, so I have chosen the second option. Once you find the above path in the registry, make a change to 'UpdateConsentMode' from '0' to '1' as shown below -