【问题标题】:Connect to TFS programmatically from vs 2017从 vs 2017 以编程方式连接到 TFS
【发布时间】:2019-06-11 12:24:25
【问题描述】:

我正在使用TFS 15.x. package

错误:

Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException: 'TF30063: 您无权访问 "https://myproject.visualstudio.com/RpaCodeReview'

Uri Repurl = new Uri("https://myproject.visualstudio.com/RpaCodeReview");
NetworkCredential netCred = new NetworkCredential(username, password);
VssBasicCredential basicCred = new VssBasicCredential(netCred);
VssCredentials tfsCred = new VssCredentials(basicCred);
TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(Repurl, tfsCred);
tpc.EnsureAuthenticated();

【问题讨论】:

  • 也许你“没有被授权访问”tfs?
  • 我有这个项目的授权
  • 我也有这个问题,最终我转而使用 TFS rest apis 与 tfs 进行通信,当他们发布它们时,因为我在升级 tfs 时维护正确的库版本没有问题。如果你想告诉我,我会发布初学者的方法。
  • 你能分享我使用 tfs apis 的代码吗?
  • REST API 非常易于使用。这是 VS2017 的documentation。这是一个 example 用于将新构建放入队列中。

标签: c# asp.net visual-studio visual-studio-2015 tfs


【解决方案1】:

这取决于您的 TFS 版本。但是,如果您尝试连接到 TFS2015 或 TFS2017,则可以这样做;

using Microsoft.TeamFoundation.Client;
using Microsoft.VisualStudio.Services.Common;
using System;
using System.Net;   

namespace TFSConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            NetworkCredential networkCredentials = new NetworkCredential(@"Domain\Account", @"Password");
            Microsoft.VisualStudio.Services.Common.WindowsCredential windowsCredentials = new Microsoft.VisualStudio.Services.Common.WindowsCredential(networkCredentials);
            VssCredentials basicCredentials = new VssCredentials(windowsCredentials);
            TfsTeamProjectCollection tfsColl = new TfsTeamProjectCollection(
                new Uri("http://XXX:8080/tfs/DefaultCollection"),
                basicCredentials);

            tfsColl.Authenticate(); // make sure it is authenticate
        }
    }
}

我不能强调确保凭据是好的!这个错误我也发生过几次。

如果上述方法不起作用,还有另一种解决方案。

  1. 关闭 Visual Studio 并转到控制面板
  2. 用户帐户 --> 管理您的凭据(在左列)
  3. 选择“Windows 凭据”
  4. 向下滚动到“通用凭据”部分并查找 您的 TFS 服务器连接
  5. 展开下拉菜单并点击“编辑”
  6. 输入您的网络密码
  7. 重新启动 Visual Studio 并重试代码

【讨论】:

  • 这是使用 Windows 凭据的权利。我可以使用 Microsoft 凭据吗?
  • 当您进入通用凭据并找到您的 TFS 连接时,只需编辑并重新输入您的密码。它应该是您用于 TFS 的密码
  • 如果这不起作用,请尝试删除您的 TFS 凭据并重新添加它。告诉我进展如何
  • 您可以尝试将管理员凭据放入您的程序中,看看是否仍然出现相同的错误?
  • 它现在可以工作了。我更改了 Microsoft 帐户中的凭据。谢谢
【解决方案2】:

除了凭据上的所有 cmets,我还发现一些存储库中的基本身份验证被阻止。

我发现最好在存储库中创建个人访问令牌 (PAT)。然后在您的连接中使用它来访问 API。

读取 tfs/devops 存储库默认集合中的项目的示例:

string PAT = "Put PAT String Here";
string RepoStore = "https://url of repo here";
string responseBody = "";

using (HttpClient 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}", "", PAT))));

    using (HttpResponseMessage response = client.GetAsync(
                RepoStore + "/_apis/projects").Result)
    {
        response.EnsureSuccessStatusCode();
        responseBody = await response.Content.ReadAsStringAsync();
    }
    Console.WriteLine(responseBody);
}

Console.ReadKey();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-15
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    • 2014-03-21
    • 2012-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多