【问题标题】:Get a list of team projects using REST api in C# for on premise TFS 2015 Update3使用 C# 中的 REST api 获取用于本地 TFS 2015 Update3 的团队项目列表
【发布时间】:2017-03-06 06:41:28
【问题描述】:

我正在尝试使用来自 c# 的 REST api 调用并遵循以下 MSDN 文档来获取所有项目:

https://www.visualstudio.com/en-us/docs/integrate/api/tfs/projects

在执行GetTeamProjects() 时,我收到以下响应:

响应 {StatusCode:404,ReasonPhrase:'未找到',版本:1.1, 内容:System.Net.Http.StreamContent,标题:

我假设错误可能是由于身份验证类型引起的。我正在通过 Basic,而我的内部部署使用 NTLM。

我正在尝试获取 TFS 项目以获取用户权限详细信息。

【问题讨论】:

    标签: c# rest tfs


    【解决方案1】:

    我只是使用它而不需要启用基本身份验证:

    var client = new WebClient();
    client.Credentials = new NetworkCredential("user", "password", "domain");
    var response = client.DownloadString("http://tfsserver:8080/tfs/teamprojectCollection/_apis/projects?api-version=2.2");
    

    【讨论】:

      【解决方案2】:

      如果您有身份验证问题,您应该得到 401 错误,而不是 404。恐怕您的代码有问题。你可以参考我下面的代码:

      using System;
      using System.Net.Http;
      using System.Net.Http.Headers;
      using System.Threading.Tasks;
      
      namespace GetTeamProjectREST
      {
          class Program
          {
              public static void Main()
              {
                  Task t = GetTeamProjectREST();
                  Task.WaitAll(new Task[] { t});
              }
              private static async Task GetTeamProjectREST()
              {
                  try
                  {
                      var username = "domain\\username";
                      var password = "password";
      
                      using (var client = new HttpClient())
                      {
                          client.DefaultRequestHeaders.Accept.Add(
                              new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
      
                          client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic",
                              Convert.ToBase64String(
                                  System.Text.ASCIIEncoding.ASCII.GetBytes(
                                      string.Format("{0}:{1}", username, password))));
      
                          using (HttpResponseMessage response = client.GetAsync(
                                      "http://tfsserver:8080/tfs/teamprojectCollection/_apis/projects?api-version=2.2").Result)
                          {
                              response.EnsureSuccessStatusCode();
                              string responseBody = await response.Content.ReadAsStringAsync();
                              Console.WriteLine(responseBody);
                          }
                      }
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.ToString());
                  }
              }
          }
      }
      

      【讨论】:

      • 您上面的代码给了我以下信息:响应 {StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent
      • 您需要在 TFS 机器上的 IIS 中启用基本身份验证。
      • 是否可以使用 NTLM 而不是基本的.. 不确定这是否是一个正确的问题。
      • 您在 IIS 中有 NTLM 身份验证吗?您需要在 IIS 中启用基本身份验证。
      • 如果您使用 VSTS 或 TFS 2017,您还可以使用 PAT 验证访问权限:visualstudio.com/en-us/docs/setup-admin/team-services/…
      猜你喜欢
      • 2016-09-03
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2016-02-13
      • 1970-01-01
      相关资源
      最近更新 更多