如何使用来自控制台应用程序的客户端api连接到TeamFoundationServer(tfs)?

时间:2014-02-19 11:11:48

标签: c# visual-studio tfs azure-devops

我正在尝试使用带有控制台应用程序的客户端API连接到visualstudio.com上托管的TeamFoundationServer,但是我收到此错误:

TF400813: Resource not available for anonymous access. Client

我的代码:

private static void Main(string[] args)
{
    Uri collectionUri = new Uri("https://MyName.visualstudio.com/DefaultCollection");

    TfsTeamProjectCollection collection =
        new TfsTeamProjectCollection(
            collectionUri,
            new System.Net.NetworkCredential(@"MeMail@gmail.com", "MyPassword"));

    WorkItemStore workItemStore = collection.GetService<WorkItemStore>(); 
}

2 个答案:

答案 0 :(得分:13)

您必须从EnsureAuthenticated()调用TfsTeamProjectCollection方法:

private static void Main(string[] args)
{
    Uri collectionUri = new Uri("https://MyName.visualstudio.com/DefaultCollection");

    NetworkCredential credential = new NetworkCredential("USERNAME", "PASSWORD");
    TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(collectionUri, credential);
    teamProjectCollection.EnsureAuthenticated();

    WorkItemStore workItemStore = teamProjectCollection.GetService<WorkItemStore>();

    WorkItemCollection workItemCollection = workItemStore.Query("QUERY HERE");

    foreach (var item in workItemCollection)
    {
        //Do something here.
    }
}

我希望它已经解决了你的问题。

答案 1 :(得分:4)

为您的帐户设置alternate credentials。您可以为命令行客户端使用备用凭据,并将其用作NetworkCredential参数。