9

我正在尝试编写连接到 TFS 并检索工作项信息的 ac# 应用程序。不幸的是,似乎所有使用 TFS SDK 的示例都使用当前用户的默认凭据(即我的域登录信息)。我找到的最接近的信息是使用TeamFoundationServer (String, ICredentials)构造函数,但是我找不到与ICredentials接口接口的合适类的任何信息(特别是因为它似乎没有使用 System.Net ICredentials,而是使用 TeamFoundationServer 特定的 ICredentials)。

是否有人对使用特定用户名/密码/域组合登录 TFS 有任何见解?

4

3 回答 3

19

以下代码应该可以帮助您:

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

域是实际域,或者在工作组情况下,它将是托管 TFS 应用程序层的服务器的名称。

于 2010-06-30T15:25:54.090 回答
14

对于 TFS 2015 和 2017,提到的对象和方法已经(或正在)弃用。

要使用特定凭据连接到 TFS:

// 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();
于 2017-01-11T14:06:01.610 回答
4

多年以后,这就是您使用 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);

如果这不起作用,请尝试通过其他Credential类传递凭据(为我工作):

// 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);
于 2015-10-05T09:07:09.357 回答