Elevated Trust in Silverlight 4

Elevated trust is one of the most important new features in SL4, my post is going to concentrated discuss about elevated trust including accessing file system/isolated storage/registry, invoking COM objects and other executable files.

Background

In Silverlight 4, Out Of Browser with elevated permission is significantly improved, now the OOB application has more privilege in accessing system resources such as the ability of accessing Isolated Storage, manipulating COM objects, accessing local registry entries, or even invoking Microsoft Speech API to phonate.

From Trusted ApplicationsYou can configure out-of-browser applications to require elevated trust. After installation, these trusted applications can bypass some of the restrictions of the security sandbox. For example, trusted applications can access user files and use full-screen mode without keyboard restrictions.

A new concept coming from .NET 4.0 called “late binding”, the C# key word: dynamic could be use to declare a undetermined type at build time, during runtime, Microsoft.CSharp.RuntimeBinder will do dynamically building.

Introduction

My post is going to concentrate on discussing about elevated trust, so read the articles below if you have any issues about creating OOB and request elevated permission.

I developed a simple Silverlight OOB demo, it will access local system resources including:

Let user choose some file(s) and then copy them to isolated storage.

Access isolated storage enumerate all files.

Create a txt file under drive C: by invoking “Scripting.FileSystemObject”, as well as read its content back.

Write registry entry under HKEY_CURRENT_USER, read registry entry under HKEY_LOCAL_MACHINE, by using “WScript.Shell”.Note: Silverlight OOB application will NOT have write permission to HKLM, it only has read permission.

Run another executable files located on the system by using “WScript.Shell”.

Phonate a sentence user input into the textbox.

Screen shot

After installing on the system, its UI is shown below (I know it is really poor… Sorry):

Implementation

The elevated permission ONLY enabled in Out Of Browser scenario, so in our Silverlight application we need check whether currently it is running out of browser:

Create a text file at “C:\WayneTestSL4Fso\WayneTest.txt”, please note: if you use System.IO.File to do such operation you won’t succeed, I guess it is because elevated trust is still not directly implemented in a lot of managed assemblies. Here in my demo, I used Scripting.FileSystemObject:

P.S. While I used “dynamic” keyword for the first time within a using statement, I was a little bit surprised, I can simply try to dispose a dynamic object without checking whether it has implemented IDisposible, hence I tried run using (dynamic x = 8 ), then I got this:

OK, let’s get back to the code implementation for reading the text file I just created.

Note 2: Intention to elevate more permission by running another EXE or script file definitely won’t succeed, for example, if I try to invoke AccessKHLM.js below from my OOB application, I will get a 80070005 error code that indicates access denied:

If you double click the Demo.js, you will succeed since you are a Windows Administrator, while “Silverlight-based applications runs in partial trust, which means they run within a security sandbox“. For more information, please refer to Trusted Application.

Conclusion

With elevated trust for Silverlight OOB applications, we can do much more than ever, it gives more confidence to develop Enterprise business applications using Silverlight technology. Yesterday, I saw Scott Guthrie posted a blog talking about Silverlight, he mentioned Microsoft will absolutely continue to work hard on Silverlight for Enterprise Business Applications (both online and OOB).