问题
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.
- Close Visual Studio and go to Control Panel
- User Accounts --> Manage your Credentials (on the left column)
- Select "Windows Credentials"
- Scroll down to the "Generic Credentials" section and look for your TFS server connection
- Expand the pull down and click "Edit"
- Enter in your network password
- 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