Alain Hufkens {Rich Interactive Applications Developer}

23May/091

Silverlight and Basic Authentication

After installing Visual Studio 2010 beta 1 I decided to play around with Silverlight 3 and the new IDE. What better example to try out than connecting a Silverlight client to the Basecamp REST API. But again like the Twitter API, the Basecamp API uses Basic Authentication (see my previous post: Flex HTTPService and Basic Authentication) and also in the case of Silverlight, it appeared to be a problem.

The first thing I tried was adding the Authorization header to the Webclient, like you can do in Actionscript.

string url =
    String.Format("https://{0}/{1}", basecampUrl, "projects.xml");
var webclient= new WebClient();
Byte[] authbytes =
    System.Text.UTF8Encoding.UTF8.GetBytes("username:password");
webclient.Headers["Authorization"] =
    "Basic " + Convert.ToBase64String(authbytes);
webclient.DownloadStringCompleted += downloadStringCompleted;
webclient.DownloadStringAsync(new Uri(url));

This didn't work because the Authorization header can't be set through code because it is restricted. Check this page for the list of restricted headers. Some can be set through a specific property, but not the Authorisation header. The error message I got was this:

System.ArgumentException:
The 'Authorization' header cannot be modified directly

Then I checked out Silverlight 3 and discovered the Credential property. You can set the Credential property to an instance of a NetworkCredential object.

string url =
    String.Format("https://{0}/{1}", basecampUrl, "projects.xml");
var webclient= new WebClient();
webclient.Credentials = new NetworkCredential("username", "password");
webclient.DownloadStringCompleted += downloadStringCompleted;
webclient.DownloadStringAsync(new Uri(url));

But too bad, the exception I got this time was even more strange:

System.NotImplementedException:
The property is not implemented by this class

Seems like there is still something missing in the beta 1 version of Silverlight 3, so please Microsoft fix this or just remove the property if it's not implemented.

So this means that the only way to get the Basic Authentication working from Silverlight at the moment is writing an extra server-side proxy class that delegates the calls and does the authentication. Or maybe there is another trick in the Silverlight Webclient class.

Comments (1) Trackbacks (0)
  1. Same problem, I need acess services that are protect using basicAthenticaton, and acess this using silverlight 3 is a great problem. Thank’s for any news…


Leave a comment


No trackbacks yet.