Tuesday, June 09, 2009

DomainProjectPicker setting UICredentialsProvider

I was playing with the TFS API for a couple of small applications I am working on. I have two different servers that I was testing the apps on. The first is the standard set up under a domain, and the second was a server with no domain configured for SSL.

Things worked great under the first scenario, however the second scenario failed with invalid credentials. So I did a quick google search and came upon several excellent posts about setting the UICredentialsProvider in the constructor of the TeamFoundationServer object. Easy right, except the DomainProjectPicker doesn't have one of those (a constructor or any public Property/Method to set the UICrendentialProvider). Not a problem until you try to get the list of Projects:

new DomainProjectPicker(DomainProjectPickerMode.AllowProjectSelect DomainProjectPickerMode.AllowMultiSelect)

Wham you get hit with credentials error again, so if you don't need to pick the projects initially you're good to use the TeamFoundationServer constructor to handle the login dialog.

So with no visible way to set the ICredentialsProvider on the picker I decided I would try some of my Microsoft connections. Hayer Casey forwarded me an email thread with the same question. The answer requires reflection as the picker does have a property to set the CredentialsProvider, but it is marked 'internal'.

We need to get the list of registered servers, and then add the UICredentialProvider to each one (I chose to use a separate method call to handle the reflection) ...

Enclosing method...

foreach(var registeredServer in RegisteredServers.GetServers())
{
SetCredentialProvider(registeredServer);
}


...Enclosing method

Add the UICredentialsProvider to all registered servers.

private static void SetCredentialProvider(TeamFoundationServer server)
{
var propertyInfo = server.GetType().GetProperty("CredentialsProvider",
BindingFlags.Public BindingFlags.NonPublic BindingFlags.Instance);


propertyInfo.SetValue(server, new UICredentialsProvider(), null);
}


After completing this everything worked beautifully. When asked if this was going to be fixed for 2010

WRT the deprecation of RegisteredServers and DomainProjectPicker, what can we expect? What will the replacements look like?


The replacement API will be similar with added functionality for dev10 servers.



Yeah, this is a little late as it will be replaced with the next release but maybe it might help somebody else who is still playing with the current API.

No comments: