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

Omi*_*ati 11 c# tfs visual-studio azure-devops

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

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>(); 
}
Run Code Online (Sandbox Code Playgroud)

Gui*_*ira 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.
    }
}
Run Code Online (Sandbox Code Playgroud)

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