我正在使用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();
我尝试了不同的解决方案。但是找不到正确的解决方案。
答案 0 :(得分:2)
这取决于您的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 :(得分:0)
在所有有关凭据的评论中,我发现某些存储库中的基本身份验证被阻止。
我发现最好在存储库中创建个人访问令牌(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();