azure-devops-node-api:使用用户名和密码进行身份验证

时间:2019-05-23 12:02:51

标签: azure-devops azure-devops-rest-api

我是node.js'azure-devops-node-api'的新手。我想连接我的收藏夹,getPersonalAccessTokenHandler(token)方法可以正常工作,但是我想使用用户名和密码进行身份验证。 getNtlmHandler(用户名,密码)会进行身份验证,但是我无法通过这种方法获取存储库。请建议我一种更好的身份验证方法

const azdev = require("azure-devops-node-api");

const collectionURL = 'https://dev.azure.com/username';
let authHandler = azdev.getNtlmHandler('username', 'password');
let connection = new azdev.WebApi(collectionURL, authHandler);

connection.connect().then(connData => {
    console.log(`Connection established successfully!!!. This is 
    ${connData.authenticatedUser.providerDisplayName}. Welcome!!!`);

    connection.getGitApi().then(vstsGit => {
        vstsGit.getRepositories('projectName').then(repos => {

            // repos is null or undefined
            console.log('There are', repos.length, 'repositories in this 
            project');
            // But When I authenticates with Token, It works fine.

        });

    });
});

1 个答案:

答案 0 :(得分:1)

查看azure-devops-node-api源代码时,您会发现有4种不同的身份验证方法。

export function getBasicHandler(username: string, password: string): VsoBaseInterfaces.IRequestHandler {
    return new basicm.BasicCredentialHandler(username, password);
}

export function getNtlmHandler(username: string, password: string, workstation?: string, domain?: string): VsoBaseInterfaces.IRequestHandler {
    return new ntlmm.NtlmCredentialHandler(username, password, workstation, domain);
}

export function getBearerHandler(token: string): VsoBaseInterfaces.IRequestHandler {
    return new bearm.BearerCredentialHandler(token);
}

export function getPersonalAccessTokenHandler(token: string): VsoBaseInterfaces.IRequestHandler {
    return new patm.PersonalAccessTokenCredentialHandler(token);
}

因为只传递用户名和密码,所以也可以使用getBasicHandler()进行身份验证。


此外,请确保以正确的方式配置安全设置。 例如,必须在组织的安全策略中切换Alternate authentication credentials ,才能对REST Api使用基本身份验证。

Azure DevOps安全参考:https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/change-application-access-policies?view=azure-devops