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

发布于 2024-09-07 07:03:40 字数 329 浏览 2 评论 0原文

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

有谁知道如何使用特定的用户名/密码/域组合登录 TFS?

I am attempting to write a c# application that connects to TFS and retrieves work item information. Unfortunately, it seems like all examples of using the TFS SDK are using the default credentials for the current user (i.e. my domain login information). The closest piece of information I found is to use the TeamFoundationServer (String, ICredentials) constructor, however I cannot find any information for a suitable class that interfaces with the ICredentials interface (especially since it seems to not be using the System.Net ICredentials but a TeamFoundationServer specific ICredentials).

Does anyone have any insight for logging into TFS with a specific username/password/domain combination?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陌生 2024-09-14 07:03:40

以下代码应该对您有帮助:

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

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

The following code should help you:

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

Domain is either the actual domain, or in a Workgroup situation, it would be the name of the server that hosts the TFS Application Tier.

饮湿 2024-09-14 07:03:40

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

For TFS 2015 & 2017, objects and methods mentioned have been (or are being) deprecated.

To connect to TFS using specific credentials:

// 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();
献世佛 2024-09-14 07:03:40

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

Years down the line, this is how you do it with 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);

If that doesn't work, try to pass the credentials through other Credential classes (worked for me):

// 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文