繁体   English   中英

如何使用特定凭据连接到C#中的TFS服务器?

[英]How do you connect to a TFS server in C# using specific credentials?

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

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

以下代码可以帮助您:

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

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

对于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();

几年后,这就是你使用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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM