简体   繁体   English

从vs 2017开始以编程方式连接到TFS

[英]Connect to TFS programmatically from vs 2017

I am using TFS 15.x. package 我正在使用TFS 15.x. package TFS 15.x. package . TFS 15.x. package

Error: 错误:

Microsoft.TeamFoundation.TeamFoundationServerUnauthorizedException: 'TF30063: You are not authorized to access " https://myproject.visualstudio.com/RpaCodeReview ' 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();

It depends on the version of your TFS. 这取决于您的TFS版本。 However, if you're trying to connect to TFS2015, or TFS2017, this will do; 但是,如果您尝试连接到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
        }
    }
}

I cannot stress enough to ensure the credentials are a-okay! 我没有足够的压力来确保凭据正常! This error has occured to me a couple times too. 我也多次发生此错误。

There is also another solution if the above doesn't work. 如果上述方法不起作用,则还有另一种解决方案。

  1. Close Visual Studio and go to Control Panel 关闭Visual Studio并转到“控制面板”
  2. User Accounts --> Manage your Credentials (on the left column) 用户帐户->管理您的凭据(在左列)
  3. Select "Windows Credentials" 选择“ Windows凭据”
  4. Scroll down to the "Generic Credentials" section and look for your TFS server connection 向下滚动到“通用凭据”部分,然后查找您的TFS服务器连接
  5. Expand the pull down and click "Edit" 展开下拉菜单,然后单击“编辑”
  6. Enter in your network password 输入您的网络密码
  7. Restart Visual Studio and retry the code 重新启动Visual Studio,然后重试代码

Along with all the comments on credentials I have found basic authentication blocked on some repositories. 连同对凭据的所有注释,我发现某些存储库中的基本身份验证被阻止。

I have found it best to create Personal Access Token (PAT) in the repository. 我发现最好在存储库中创建个人访问令牌(PAT)。 Then use that in you connections to access the APIs. 然后在连接中使用它来访问API。

Example to read what projects are in the default collection of a tfs/devops repo: 读取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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM