4

I'm trying to connect to TFS using:

TfsTeamProjectCollection teamProjectCollection 
  = new TfsTeamProjectCollection(new Uri(teamProjectCollectionUrl));

but with credentials. For example I want to connect to "http://tfs.mydomain.com/sandbox/" with credentials "myusername" and "mypassword"...what is the correct way to set up credentials for this?

After getting connected using the credentials I know how to do everything else i need.

Nived
  • 1,804
  • 1
  • 15
  • 29

2 Answers2

3

The NetworkCredential class implements the ICredentials interface and will allow you to authenticate against AD.

NetworkCredential cred = new NetworkCredential("Username", "Password", "Domain");

Pass the NetworkCredential object as part of your constructor.

See here for more information on the new constructor you must call: https://msdn.microsoft.com/en-us/library/ff737302.aspx

Best practice nowadays is to use TfsClientCredentials class, which would look like this:

NetworkCredential cred = new NetworkCredential("Username", "Password", "Domain");
BasicAuthCredential basicCred = new BasicAuthCredential(cred);
TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);

Personally I've always just used the NetworkCredential.

ravibhagw
  • 1,740
  • 17
  • 28
0

MSDN talks about using credentials to connect to TFS here:

You can use an ICredentials object when you connect to Team Foundation Server to specify the identity to impersonate. This strategy does not require special permissions, but you must be able to obtain the password of the identity to create the ICredentials object.

You can also specify an implementation of ICredentialsProvider when you connect to Team Foundation Server to handle requests for new credentials. The system calls the implementation of ICredentialsProvider that you specify to request new credentials when the credentials that are specified by the ICredentials object are not successfully authenticated or authorized to perform the operation.

To prompt the user for credentials, you can use the UICredentialsProvider class, which implements ICredentialsProvider by displaying a logon dialog box to prompt the user for new credentials.

EDIT: Dig some more reading and the new, most correct way to do this is to use

TfsClientCredentials

which is described here

Pseudonym
  • 2,052
  • 2
  • 17
  • 38
  • I don't really see where you set the credentials you are using in TfsClientCredentials...like how do I set the password and username? – Nived Apr 15 '15 at 13:51
  • 1
    https://msdn.microsoft.com/en-us/library/bb130172(v=vs.90).aspx this article explains it, and if you want take a look at a good project using tfs https://taskcardcreator.codeplex.com/ is a great sample of things that could be done with it. and there is also this answer here at stackoverflow http://stackoverflow.com/questions/3150260/how-do-you-connect-to-a-tfs-server-in-c-sharp-using-specific-credentials – Thorarins Apr 15 '15 at 13:58