Connect to TFS programmatically from vs 2017

﹥>﹥吖頭↗ 提交于 2020-01-15 11:55:07

问题


I am using TFS 15.x. package.

Error:

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

回答1:


It depends on the version of your TFS. However, if you're trying to connect to TFS2015, or TFS2017, this will do;

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
  2. User Accounts --> Manage your Credentials (on the left column)
  3. Select "Windows Credentials"
  4. Scroll down to the "Generic Credentials" section and look for your TFS server connection
  5. Expand the pull down and click "Edit"
  6. Enter in your network password
  7. Restart Visual Studio and retry the code



回答2:


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. Then use that in you connections to access the APIs.

Example to read what projects are in the default collection of a tfs/devops repo:

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();


来源:https://stackoverflow.com/questions/54233619/connect-to-tfs-programmatically-from-vs-2017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!