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.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:
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.
But too bad, the exception I got this time was even more strange:
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.
September 6th, 2009 - 01:50
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…