2010-06-30 61 views
6

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

有没有人对使用特定用户名/密码/域组合登录到TFS有任何洞察力?

回答

16

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

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

domain是实际的域或工作组中的情况下,这将是承载TFS应用层的服务器的名称。

+0

真棒工作!谢谢。 – KallDrexx 2010-06-30 15:51:29

+2

请注意'TeamFoundationServer'已被弃用,以支持'TfsConfigurationServer',但是这个代码也适用于它。 – 2014-07-23 22:25:50

3

十年过去了,你这是怎么用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); 
+0

为什么不直接将'NetworkCredential'传递给'TfsTeamProjectCollection',而不是创建两个不必要的对象? – 2015-10-05 11:10:58

+0

嗯,我试过了,而且我得到臭名昭着的'基本身份验证需要安全连接到服务器'的错误。即使在清除Windows Credential Manager条目之后,它仍然在发生。然后,我尝试了一下,直到这使它工作。也许你是对的,你不需要它:-)只是想拯救别人的麻烦 – Heliac 2015-10-05 11:46:43

+0

感谢您的反馈 - 这是意想不到的,所以我会确保我们看看它。 – 2015-10-05 15:06:26

7

对于TFS 2015 & 2017,上述目的和方法已经(或正在)弃用。连接到TFS使用特定的凭据:

// For TFS 2015 & 2017 

    // Ultimately you want a VssCredentials instance so... 
    NetworkCredential netCred = new NetworkCredential(@"DOMAIN\user.name", @"Password1"); 
    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(); 

希望这会有所帮助。

相关问题