Someone asked how to get the login dialog if the user fails to authenticate. The code to do that is shown below. I copied the call to the constructor from the basic API example, and I’ve added code to create and pass a UICredentialsProvider object to the TeamFoundationServer constructor.
Also, I’m now calling EnsureAuthenticated(). The purpose of that call is to make sure that the user is able to authenticate with the server. If not, the TeamFoundationServer object will use the UICredentialsProvider to display a login dialog. If the user authenticates successfully after entering a user name and password, the program will continue execution. If the user clicks the Cancel button in the login dialog, perhaps after failing to enter a valid user name and password, the EnsureAuthenticated() method will throw TeamFoundationServerUnauthorizedException.
If you look at the TeamFoundationServer class, you will notice that there are two authentication methods to call: Authenticate() and EnsureAuthenticated(). The difference is that Authenticate() will always call the server, and EnsureAuthenticated() will only call the server if the user has not already authenticated, resulting in no performance penalty for calling it multiple times.
Only Authenticate() and EnsureAuthenticated() will display the login dialog. Any other method calls that fail due to the user not authenticating will result in TeamFoundationServerUnauthorizedException being thrown.
// Get a reference to our Team Foundation Server.
TeamFoundationServer tfs = new TeamFoundationServer(tfsName, new UICredentialsProvider());
// Authenticate with the server if we haven’t already (in this example, we haven’t).
// If authentication fails, the user will see the login dialog, as a result of
// UICredentialsProvider() being specified above.
[Update 3/20/06] Fixed typo. Thanks, Tim.