Silverlight 5 Beta - Run Elevated Trust Applications in Browser

Posted by: Pravinkumar Dabade , on 5/14/2011, in Category Silverlight 2, 3, 4 and 5
Views: 46062
Abstract: 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.

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 -

Silverlight 5 Trusted App

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>             <RowDefinition Height="44*" />             <RowDefinition Height="44*" />             <RowDefinition Height="44*" />             <RowDefinition Height="170*" />         </Grid.RowDefinitions>         <StackPanel Orientation="Horizontal" Grid.Row="0">             <TextBlock Text="TO - :" Foreground="Yellow" FontSize="14"         HorizontalAlignment="Center" VerticalAlignment="Center"/>             <TextBox x:Name="txtTO" Width="350" Height="30"/>         </StackPanel>         <StackPanel Orientation="Horizontal" Grid.Row="1">             <TextBlock Text="CC - :" Foreground="Yellow" FontSize="14"         HorizontalAlignment="Center" VerticalAlignment="Center"/>             <TextBox x:Name="txtCC" Width="350" Height="30"/>         </StackPanel>         <StackPanel Orientation="Horizontal" Grid.Row="2">             <TextBlock Text="Subject - :" Foreground="Yellow" FontSize="14"         HorizontalAlignment="Center" VerticalAlignment="Center"/>             <TextBox x:Name="txtsubject" Width="315" Height="30"/>         </StackPanel>         <StackPanel Orientation="Vertical" Grid.Row="3">             <TextBox x:Name="txtmessage" Width="400" Height="120"         HorizontalAlignment="Left" VerticalAlignment="Top" />             <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">                 <Button x:Name="btnSend" Content="Send" Height="30" Width="100"          Click="btnSend_Click"/>                 <Button x:Name="btncancel" Content="Cancel" Height="30" Width="100" />             </StackPanel>         </StackPanel> </Grid>

Now let's import a namespace to our code behind -

using System.Runtime.InteropServices.Automation;

Silverlight 5 Beta Outlook

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

NotSupportedException

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 -

Update Consent Mode

Step 2: Now the second step is to Sign the .XAP file of our Silverlight application with code signing certificate. Right click t Sheilverlight application and go to properties. Choose ‘Signing from the left hand side’ and check the checkbox 'Sign the .XAP file' as shown below -

Silverlight Sign XAP

Now click on the button 'Create Test Certificate'. Enter the password and confirm password and click 'ÓK' button.

Step 3: Now click on the 'More Details' button highlighted in blue colour. This will show you a 'Certificate' window. Click on the 'Install Certificate' button as shown below -

Certificate Information

Clicking on the button brings up the 'Certificate Import wizard'. Click on the 'Next' button and choose 'Place all certificates in the following store'. Click on the Browse button. This will show you a 'Certificate Store'. Choose 'Trusted Publisher' and finish the wizard. Now repeat the same step to install this certificate in 'Trusted Root Certification Authorities'.

That's it! You are done with the configuration. Now hit 'F5' to launch the Silverlight application in a browser. Fill the details and click on the 'Send' button. This will show you a Internet Security dialog box. Click on the 'Allow' button and you will see the output as shown below -

Silverlight COM Outlook

Summary - In this article we have seen how to run Silverlight 5 Elevated Trust application within the browser.

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Pravinkumar, works as a freelance trainer and consultant on Microsoft Technologies. He is having over 10 years of experience in IT and is also a Microsoft Certified Trainer(MCT). He has conducted various corporate trainings on all versions of .NET Technologies including .NET, SharePoint Server, Microsoft SQL Server, Silverlight, ASP.NET, Microsoft PerformancePoint Server 2007 (Monitoring). He is passionate about learning new technologies from Microsoft. You can contact Pravinkumar at dabade[dot]pravinkumar[attherate]gmail[dot]com


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by Geoff on Saturday, May 14, 2011 4:04 AM
Don't you mean Elevated rather than Evaluated?
Comment posted by Geoff on Saturday, May 14, 2011 4:21 AM
Don't you mean Elevated rather than Evaluated?
Comment posted by Pravinkumar on Saturday, May 14, 2011 11:56 PM
Hi Geoff,

Thank you for addressing the typo mistake. We will correct the same.

Pravin
Comment posted by Trix on Thursday, February 9, 2012 6:40 AM
Thanks for the post! It was a great help for me. I think the registry entry you mentioned (UpdateConsentMode) is wrong. It didn't work for me. I found this Microsoft article http://msdn.microsoft.com/en-us/library/gg192793%28v=vs.95%29.aspx and they say, the key, which has to be added is AllowElevatedTrustAppsInBrowser with value 1. I changed this and it worked!
Comment posted by Bijay on Tuesday, July 17, 2012 5:42 AM
Hi Praveen,

We have implemented in our application as you suggested above and it is working fine for the local host. However when we access the same URL remotely then we are experiencing the below error.

“This Operation is not supported in the current context”

Another issue is we need to add key “AllowElevatedTrustAppsInBrowser” into client registry which is a security issue. How can we bypassing the security restrictions applied by OS
Comment posted by Touqeer Tariq on Wednesday, February 20, 2013 12:10 PM
Hi Praveen,

Your article is really informative. My questions is that HOW I am going to tell me clients to enable the Registry key on their system? Is there a way that I could set the registry key from the silverligt? Otherwise I'd have to ask them to download any small exe and run it which would enable the registry kery. Any other solutions? I'll be really thankful.

Regards,
Touqeer

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel