在 C# 中使用凭据连接到 TFS

Connect to TFS using credentials in C#

我正在尝试使用以下方式连接到 TFS:

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

但有凭据。例如,我想使用凭据 "myusername" 和 "mypassword" 连接到“http://tfs.mydomain.com/sandbox/”...为此设置凭据的正确方法是什么?

使用凭据连接后,我知道如何做我需要的所有其他事情。

MSDN 谈论使用凭据连接到 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.

编辑:多读一些书,新的、最正确的方法是使用

TfsClientCredentials

描述的是here

NetworkCredential class 实现了 ICredentials 接口并将允许您针对 AD 进行身份验证。

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

将 NetworkCredential 对象作为构造函数的一部分传递。

有关必须调用的新构造函数的详细信息,请参阅此处:https://msdn.microsoft.com/en-us/library/ff737302.aspx

现在的最佳做法是使用 TfsClientCredentials class,它看起来像这样:

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

我个人一直只使用 NetworkCredential。