Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 636
KallDrexx Avatar asked Jun 30 '10 14:06

KallDrexx


People also ask

How do I connect to a TFS server?

Select the Manage Connections button in Team Explorer to open the Connect page. Choose Connect to Team Project to select a different organization, TFS, or project to connect to. Select the projects to work on. If it's your first time connecting, add TFS to the list of recognized servers.

What is TFS server URL?

The TFS web portal allows you to manage source code, work items, builds, test efforts, machines and test runs. You can open the web portal from a connected project in Visual Studio or from a web browser using the following URL: http://ServerName:8080/tfs/CollectionName/ProjectName copy.


2 Answers

For TFS 2015 & 2017, objects and methods mentioned have been (or are being) deprecated.

To connect to TFS using specific credentials:

// For TFS 2015 & 2017

// Ultimately you want a VssCredentials instance so...
NetworkCredential netCred = new NetworkCredential(@"user.name", @"Password1", "DOMAIN");
WindowsCredential winCred = new WindowsCredential(netCred);
VssCredentials vssCred = new VssClientCredentials(winCred);

// Bonus - if you want to remain in control when
// credentials are wrong, set 'CredentialPromptType.DoNotPrompt'.
// This will thrown exception 'TFS30063' (without hanging!).
// Then you can handle accordingly.
vssCred.PromptType = CredentialPromptType.DoNotPrompt;

// Now you can connect to TFS passing Uri and VssCredentials instances as parameters
Uri tfsUri = new Uri(@"http://tfs:8080/tfs");
var tfsTeamProjectCollection = new TfsTeamProjectCollection(tfsUri, vssCred);

// Finally, to make sure you are authenticated...
tfsTeamProjectCollection.EnsureAuthenticated();
like image 125
sw4DPeople Avatar answered Oct 19 '22 22:10

sw4DPeople


Years down the line, this is how you do it with TFS 2013 API:

// Connect to TFS Work Item Store
ICredentials networkCredential = new NetworkCredential(tfsUsername, tfsPassword, domain);
Uri tfsUri = new Uri(@"http://my-server:8080/tfs/DefaultCollection");
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsUri, networkCredential);
WorkItemStore witStore = new WorkItemStore(tfs);

If that doesn't work, try to pass the credentials through other Credential classes (worked for me):

// Translate username and password to TFS Credentials
ICredentials networkCredential = new NetworkCredential(tfsUsername, tfsPassword, domain);
WindowsCredential windowsCredential = new WindowsCredential(networkCredential);
TfsClientCredentials tfsCredential = new TfsClientCredentials(windowsCredential, false);

// Connect to TFS Work Item Store
Uri tfsUri = new Uri(@"http://my-server:8080/tfs/DefaultCollection");
TfsTeamProjectCollection tfs = new TfsTeamProjectCollection(tfsUri, tfsCredential);
WorkItemStore witStore = new WorkItemStore(tfs);
like image 37
Riegardt Steyn Avatar answered Oct 20 '22 00:10

Riegardt Steyn