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
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 -
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;
Now run your application and click on the 'Send' button. You will get an exception as shown below -
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 -
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 -
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 -
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 -
Summary - In this article we have seen how to run Silverlight 5 Elevated Trust application within the browser.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
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