A few months ago, I investigated Silverlight 3 Beta on support for Credentials.
There are two classes that give a little bit of notion that they are supporting Credentials. Both WebClient and WebRequest have a property Credentials of type ICredentials. Let’s try them one by one.
WebRequest credentials support?
First we need to have some code to request the content behind an url and assign a set of credentials to the request.
WebRequest request = HttpWebRequest.Create("http://mark.mymonster.nl");
request.Credentials = new NetworkCredential("username", "password");
Very straightforward code which you can write for the full CLR as well. This code doesn’t even contain the execution of the request.
Yes it fails. If you try to set the de Credentials you will get a NotImplementedException. Sad, so we have not support for credentials in the WebRequest.
WebRequest credentials support outcome: Negative
WebClient credentials support?
Next class with a Credentials property, WebClient. First let’s write some code.
var client = new WebClient
{
Credentials =
new NetworkCredential("username", "password")
};
client.DownloadStringCompleted += client_DownloadStringCompleted;
client.DownloadStringAsync(new Uri("http://mark.mymonster.nl"));
Of course if anything goes wrong we can see it in the DownloadStringCompleted. From QuickWatch, we can see what happened.
Hmm, I could have guessed that WebClient internally makes use of WebRequest. So also WebClient doesn’t support credentials.
WebClient credentials support outcome: Negative
Hmm, last option we have: Adding the Authorization header manually
In the past I also tried to add the Authorization header manually. I won’t go into the details but, the implementation of the Credentials property would mean that some data would be put into the Authorization header.
Sad, this still doesn’t work. The ‘Authorization’ header cannot be modified directly.
Manually adding Authorization header outcome: Negative
Conclusion
Sadly both WebClient and WebRequest don’t support Credentials yet. I would have thought it did support, basically because they supplied a property called Credentials that wasn’t in the Silverlight 2 release. I’m just wondering if they just didn’t get the implementation in place on time for the RTW of Silverlight 3.
Trying to do it manually isn’t supported as well. I’m just wondering why do we have a property Credentials on those objects? Would be nice to get some response from the Silverlight team on this one.
Ps. This article is cross posted on: Mark Monster’s blog and Silverlight Help.