How do you connect to a TFS server in C# using specific credentials?

KallDrexx picture KallDrexx · Jun 30, 2010 · Viewed 21.1k times · Source

I am attempting to write a c# application that connects to TFS and retrieves work item information. Unfortunately, it seems like all examples of using the TFS SDK are using the default credentials for the current user (i.e. my domain login information). The closest piece of information I found is to use the TeamFoundationServer (String, ICredentials) constructor, however I cannot find any information for a suitable class that interfaces with the ICredentials interface (especially since it seems to not be using the System.Net ICredentials but a TeamFoundationServer specific ICredentials).

Does anyone have any insight for logging into TFS with a specific username/password/domain combination?

Answer

Robaticus picture Robaticus · Jun 30, 2010

The following code should help you:

NetworkCredential cred = new NetworkCredential("Username", "Password", "Domain");
tfs = new TeamFoundationServer("http://tfs:8080/tfs", cred);
tfs.EnsureAuthenticated();

Domain is either the actual domain, or in a Workgroup situation, it would be the name of the server that hosts the TFS Application Tier.