How do you connect to a TFS server in C# using specific credentials?
Solution 1
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.
Solution 2
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();
Solution 3
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);
KallDrexx
Updated on July 09, 2022Comments
-
KallDrexx almost 2 years
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 theICredentials
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?
-
Admin almost 10 yearsNote that
TeamFoundationServer
has been deprecated in favor ofTfsConfigurationServer
, but this code is valid for it too. -
Edward Thomson over 8 yearsWhy don't you just pass the
NetworkCredential
straight to theTfsTeamProjectCollection
, instead of creating two unnecessary objects? -
Riegardt Steyn over 8 yearsWell, I tried that, and I was getting the infamous
Basic authentication requires a secure connection to the server
error. Even after clearing my Windows Credential Manager entries it was still happening. Then I experimented a bit until this made it work. Maybe you're right and you don't need it :-) Just wanted to save someone else the trouble -
Edward Thomson over 8 yearsThanks for the feedback - that's unexpected, so I'll make sure that we look into it.
-
Riegardt Steyn over 8 years@EdwardThomson thank you for your time. I can confirm using the NetworkCredential directly does work now. I honestly don't know why it didn't in the first place. My Credential Manager only had an entry for
virtualapp/didlogical
(Windows Live) and some local servers. I removed all of them anyway. I know I was trying other classes likeBasicAuthCredential
; maybe I just used the wrong credential type from the get go? Anyway, I'm adding an edit to my answer. Keep well! -
ΩmegaMan over 5 yearsFYI pull in
Microsoft.TeamFoundationServer.ExtendedClient
from Nuget.