首页 > 解决方案 > 从 vs 2017 以编程方式连接到 TFS

问题描述

我正在使用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();

标签: c#asp.netvisual-studiovisual-studio-2015tfs

解决方案


这取决于您的 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 并重试代码

推荐阅读