Securing Silverlight Application and WCF Service using ASP.NET Authentication Techniques

Security is an issue that experts are discussing since the birth of Silverlight. A typical enterprise Silverlight application will consist of one or more Silverlight pages communicating with one or more WCF services. There are three major security concerns here:

  1. Silverlight 2 supports communication with a WCF service in simple text (the basicHttpBinding), so anyone can use a packet sniffer (e.g. Fiddler) to read our data.
  2. Anyone can access our WCF service, call its methods and get our data.
  3. Anyone can download our Silverlight application (the .xap file), open it (since it is simple a zip file), extract the DLLs and use Reflector to read all our code.

The first problem can be solved by securing the transmission via transport security (using https protocol). More on this can be found at MSDN here. In this post, I will try to address the last two issues.

The good thing is that the Silverlight application and WCF service is hosted inside an ASP.NET website and luckily ASP.NET provides good tools around authentication so we can apply ASP.NET authentication techniques to secure those. The approach I like is to secure the entire ASP.NET web application using either Windows or Forms authentication and deny all anonymous users. This can be done by configuring the web.config as:

  <authentication mode="Windows"/> <!-- or Forms -->
  <authorization>
    <deny users="?"/>
  </authorization>

This way, if anyone tries to access the WCF service, or download the Silverlight .xap file, or view a page inside the ASP.NET website, the ASP.NET engine authenticates the request and only authorized users are able to view the application or use the service.

So now, if our application is configured to use Windows authentication, the ASP.NET engine authenticates the request via integrated windows authentication. If it succeeds, users are automatically redirected to the Silverlight application; otherwise they get a HTTP 401.1 – Unauthorized message.

And, if our application is configured to use Forms authentication, the ASP.NET engine takes the user to an aspx login page. Once the user is validated (we can use either ASP.NET built-in authentication or any custom implementation to authenticate the user), he/she is redirected to the Silverlight application.

To observe this, you can download this application (Be sure to first rename the file to zip; this is a WordPress requirement) and toggle its authentication technique between Windows and Forms using web.config.

Note that the application also demonstrates how to get the logged in user in Silverlight using a WCF service call. The key is that we can use System.Web.HttpContext.Current.User to get the current user principal if the WCF service is running in ASP.NET compatibility mode.


Screenshot of demo application

At this point, we have made sure that our application and WCF service is only accessible to authorized users. But the problem still exists to a small extent, although narrowed down to authorized users instead of general public. To further secure our application, we need to use some .NET obfuscater. This will ensure that no one, including authorized users, will be able to decompile our code using .NET reflector. And, to further enhance our WCF service security, we need to implement declarative or imperative security checks for our service methods. As with typical ASP.NET applications, we may need a custom implementation of IPrincipal and IIdentity for securing the WCF service using declarive security attributes.

That’s all. Let me know what you do think in the comments section below.

7 Responses to “Securing Silverlight Application and WCF Service using ASP.NET Authentication Techniques”

  1. Deploying a Silverlight Application and WCF Service to IIS « Mehroz’s Experiments Says:

    [...] Deploying a Silverlight Application and WCF Service to IIS While deploying a Silverlight application on IIS today, I learned several new things. Let me express my observations; This post is going to describe the security settings for WCF service in web.config. To start, I assume that you are using either Windows or Forms authentication and denying all the anonymous users as described in a previous post. [...]

  2. Gabriel Says:

    Hi, How can I access ADO.net Data Services securily from within SL2 using ASP.net auth services?
    Thanks in advance

    Gabriel

  3. Syed Mehroz Alam Says:

    @Gabriel,

    Since ADO.Net Data Services, like WCF services, are hosted inside the ASP.NET web application, so things are much similar. Since you are already using ASP.NET authentication services, make sure that you deny all anonymous users. This way, if any unauthenticated user try to access your ADO.NET data service, he will be redirected to the login page.
    Hope that helps.

  4. 15 Excellent And Useful Microsoft Silverlight Tutorials & Resources @ SmashingApps Says:

    [...] Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques [...]

  5. FreeDownloadSecrets.com » Blog Archive » 15 Excellent And Useful Microsoft Silverlight Tutorials & Resources Says:

    [...] Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques [...]

  6. De Web Times - Share Your Resources » Blog Archive » 15 Useful Microsoft Silverlight Tutorials & Resources Says:

    [...] Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques [...]

  7. 15 Excellent And Useful Microsoft Silverlight Tutorials & Resources « N3T.ir - Web Resources, Web Design News & Tips, Open Source Says:

    [...] Securing Silverlight Application and WCF Service using ASP.Net Authentication Techniques [...]


Leave a Reply