从云服务访问 VSTS 团队项目集合

Accessing VSTS Team Project Collection from cloud service

我的云服务正在尝试使用 TFS API 在 VSTS 中创建一个新团队。这是代码片段 -

var serverUrl = TfsTeamProjectCollection.GetFullyQualifiedUriForName("https://vsts_instace_name.visualstudio.com/DefaultCollection");
TfsTeamProjectCollection teamProjectCollection = new TfsTeamProjectCollection(serverUrl, 
                                                 new AadCredential(username, password));
teamProjectCollection.EnsureAuthenticated();

var vssCredentials = teamProjectCollection.ClientCredentials.ConvertToVssCredentials(teamProjectCollection.Uri);
ProjectHttpClient projectHttpClient = new ProjectHttpClient(teamProjectCollection.Uri,   
                                     vssCredentials);

var projectInfo = projectHttpClient.GetProject("TestProject");
var teamService = teamProjectCollection.GetService<TfsTeamService>();
var team = teamService.CreateTeam(projectInfo.Id.ToString(), "NewTeam", string.Empty, null);

该代码在本地环境中运行正常,但在云端运行失败。在它试图检索 projectInfo 的行抛出一个异常。例外是 - TF400813: Resource not available for anonymous access. Client authentication required.

我需要修改验证码吗?

您可以尝试使用基本身份验证进行身份验证:

public static void BasicAuthSoapSample()
{
    // Authenticate using Basic Authentication
    NetworkCredential netCred = new NetworkCredential(basicAuthUsername, password);
    BasicAuthCredential basicCred = new BasicAuthCredential(netCred);
    TfsClientCredentials tfsCred = new TfsClientCredentials(basicCred);
    tfsCred.AllowInteractive = false;
    using (TfsTeamProjectCollection tpc = new TfsTeamProjectCollection(new Uri(collectionUri), tfsCred))
    {
        tpc.Authenticate();
        Console.WriteLine(tpc.InstanceId);
    }
}

创建个人访问令牌或启用备用身份验证凭据,然后尝试使用以下代码:

string tfsurl = "https://xxxxxx.visualstudio.com/";
string projname = "project";
NetworkCredential nc = new NetworkCredential("alternativeusername", "alternativepasswordorpersionalaccesstoken");
BasicAuthCredential bac = new BasicAuthCredential(nc);
TfsClientCredentials tfcc = new TfsClientCredentials(bac);
tfcc.AllowInteractive = false;
TfsTeamProjectCollection ttpc = new TfsTeamProjectCollection(new Uri(tfsurl), tfcc);
ttpc.Authenticate();
ProjectHttpClient phc = ttpc.GetClient<ProjectHttpClient>();
TeamProject pi = phc.GetProject(projname).Result;