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

时间:2016-09-13 10:52:48

标签: tfs azure-cloud-services

我的云服务正在尝试使用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.

我是否需要对身份验证代码进行任何更改?

2 个答案:

答案 0 :(得分:1)

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

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;

答案 1 :(得分:0)

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

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