VSTS身份验证中具有访问令牌凭证的VssClientCredentials

时间:2018-06-21 10:29:25

标签: tfs azure-devops

我正在从VSTS中读取用户的许可信息,但未使用访问令牌凭据对其进行身份验证。

string accessToken = "{AccessTokenHere}";
                VssOAuthAccessTokenCredential accessTokenCredentials = new VssOAuthAccessTokenCredential(new VssOAuthAccessToken(accessToken));
                var credentials = new VssClientCredentials(accessTokenCredentials);

                VssConnection connection = new VssConnection(new Uri(this.ServerUri), credentials);
                var licensingHttpClient = connection.GetClient<LicensingHttpClient>();
                var accountEntitlement = licensingHttpClient.GetAccountEntitlementAsync().Result;
                var license = accountEntitlement.License;

3 个答案:

答案 0 :(得分:0)

尝试以下代码:

        String collectionUri = "https://{account}.visualstudio.com";
        VssBasicCredential creds = new VssBasicCredential("", personalaccesstoken);
        VssConnection connection = new VssConnection(new Uri(collectionUri), creds);
        var licensingHttpClient = connection.GetClient<LicensingHttpClient>();
        var accountEntitlement = licensingHttpClient.GetAccountEntitlementAsync().Result;
        var license = accountEntitlement.License;

关于个人访问令牌,您可以参考以下链接:

https://docs.microsoft.com/en-us/vsts/accounts/use-personal-access-tokens-to-authenticate?view=vsts

答案 1 :(得分:0)

我认为,当您拥有来自oauth2身份验证的承载令牌时,可以跳过以下行:

// skip this line in your code: var credentials = new VssClientCredentials(accessTokenCredentials);

对我来说,此代码有效:

VssOAuthAccessTokenCredential credentials = new VssOAuthAccessTokenCredential(AccessToken);
VssConnection connection = new VssConnection(new Uri("https://dev.azure.com/[your org]"), credentials);
var client = connection.GetClient<ProjectHttpClient>();
var projects = client.GetProjects().Result;

还请确保您为想要达到的目标在应用程序中注册了正确的作用域。

答案 2 :(得分:0)

这就是我使用个人访问令牌的方式。

VssBasicToken token = new VssBasicToken(new NetworkCredential("", "string-token"));

VssCredentials credentials = new VssBasicCredential(token);

VssConnection connection = new VssConnection(connectionUri, credentials);