Silverlight 5 Beta – Unrestricted File Access in Browser

In this post, we will explore Silverlight 5 Beta unrestricted file access within a browser with elevated trust. In a previous article – ‘Silverlight 5 Beta - Run Elevated Trust Applications in Browser’ on http://www.dotnetcurry.com/, I have explained how to configure Silverlight 5 Elevated Trust application within browser.

To start with, let’s first design a Silverlight application with the name ‘SL5UnrestrictedFileAccess’ and choose Silverlight 5 version as shown below –

Silverlight 5 app

Design MainPage.xaml. Replace the <Grid></Grid> with the following code –

Silverlight 5 app Unrestricted Access

Now let's write the following code in the click event of 'Write To File' as shown below –

clip_image006

The above code will create a file on 'C:\' and write a message which we will entered using a Textbox. Now let's write the code to read the file on the click event of 'Read From File' button click event, as shown below -

clip_image008

Now let's run the application and click the 'Write to File' button by writing the message in our textbox. You will get an exception as shown below -

clip_image010

Let's configure the application to run 'Out - Of - Browser'. Also make your application to run in Elevated Trust. Hit 'F5' and run your application. Click the button to test the application. You will see the file is created on 'C:\Test' folder. Surprised? It will run because it is running our application under 'localhost' URL.

Let's try the same by changing the URL. Instead of localhost, let's write your machine name and now run the application. This time, It will throw an exception as below -

clip_image011

To run the application in a browser with elevated trust, you will have to configure the application as below -

1) Change the registry entry to run evaluated trust application in browser.
2) Configure the application to run 'Out - Of - Browser'. Also make your application to run in Elevated Trust.
3) Sign the .XAP file with test certificate.
4) Deploy the certificate in 'Trusted Publisher' and 'Trusted Root Certification Authorities'.

All the above steps are explained in depth in my article Silverlight 5 Beta - Run Evaluated Trust Applications in Browser’ on http://www.dotnetcurry.com/

Once you complete the configuration steps listed above, run the application and click the button 'Write To File' and you will see the file created on your 'C:\Test' folder with the input message as shown below -

clip_image013

To test this application other than 'localhost' you will have to configure the application in IIS.




No comments: