【问题标题】:Creating a AD Security group in Azure AD in Nodejs在 Nodejs 中的 Azure AD 中创建 AD 安全组
【发布时间】:2020-08-17 21:30:06
【问题描述】:

我正在尝试在我的 azure AD 目录中创建一个安全组,并将 b2c 用户(已登录)添加到该组。

msRestAzure.loginWithServicePrincipalSecret('72a3198b-', '-FvUZZ4G', tenantId, { tokenAudience: 'graph' }, function (err, credentials, subscriptions) {
    if (err) console.log("error: " + err);
    else {
        console.log(util.inspect(credentials));
    }
    var client = new graphRbacManagementClient(credentials, tenantId);
    client.groups.list({}, function(err, result){
        if(err){
            console.log('Could not list groups', err)
        }
        else {
            console.log("result: " + result);
        }
    })

我已在 Azure 上启用 API 权限......

虽然我成功使用我的 SP 进行身份验证,但无论我尝试创建新组还是列出现有组等,我都会收到 403 作为响应。

关于我在这里做错了什么有什么想法吗?

更新:我更改了代码并改用msRestAzure.loginWithUsernamePassword。我使用的用户名和密码用于具有管理员权限的用户。更改后,代码工作并创建/列出了组。所以我的猜测是,SP 需要某种我尚未启用的访问/API 权限。我只是不知道是什么..

【问题讨论】:

标签: node.js azure azure-ad-b2c azure-ad-graph-api


【解决方案1】:

graphRbacManagementClient不支持调用MS graph api。它用于 Azure AD Graph API。

这是您可以关注的code sample。它使用 microsoft-graph-client 调用 Microsoft Graph。

使用客户端凭据流获取访问令牌:

const request = require("request");

const endpoint = "https://login.microsoftonline.com/[Tenant]/oauth2/v2.0/token";
const requestParams = {
    grant_type: "client_credentials",
    client_id: "[ApplicationID]",
    client_secret: "[Key]",
    scope: "https://graph.microsoft.com/.default"
};

request.post({ url:endpoint, form: requestParams }, function (err, response, body) {
    if (err) {
        console.log("error");
    }
    else {
        console.log("Body=" + body);
        let parsedBody = JSON.parse(body);         
        if (parsedBody.error_description) {
            console.log("Error=" + parsedBody.error_description);
        }
        else {
            console.log("Access Token=" + parsedBody.access_token);
        }
    }
});

调用 MS Graph API:

// list groups
function testListGroupGraphAPI(accessToken) {
    request.get({
        url:"https://graph.microsoft.com/v1.0/groups",
        headers: {
          "Authorization": "Bearer " + accessToken
        }
    }, function(err, response, body) {
        console.log(body);
    });
}

// create group    
function testCreateGroupGraphAPI(accessToken) {
    request.post({
        url:"https://graph.microsoft.com/v1.0/groups",
        headers: {
           "Authorization": "Bearer " + accessToken
        },
        json: {
            "description": "Self help community for library",
            "displayName": "Library Assist",
            "groupTypes": [
                "Unified"
            ],
            "mailEnabled": true,
            "mailNickname": "library",
            "securityEnabled": false
            }
    }, function(err, response, body) {
        console.log(body);
    });
}

更多详情请见here


Azure AD Graph的权限在这里:

【讨论】:

  • 根据我的帖子,我正在尝试使用服务原则在我的 Azure AD 目录中创建一个组。我不想获取有关用户等的任何信息。我​​应该使用什么 sdk?任何示例代码将不胜感激。
  • @SeanD 您可以使用访问令牌在没有用户信息的情况下调用图形 api。我更新了对示例代码的回答。
  • @SeanD 添加此权限(导航到 Azure Active Directory Graph-> Application-> Directory.ReadWrite.All)。您可以使用graphRbacManagementClient 调用 api
  • 从我的屏幕截图中可以看到,添加了 Directory.ReadWrite.All。仍然得到 403。!@#$
  • @SeanD 我的意思是 Azure Active Directory Graph 的权限,而不是 Microsoft Graph 的权限。 Azure Active Directory Graph 是较旧的 MS Graph。如果您仍想使用 MS Graph,我的答案中的代码会有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-10
  • 1970-01-01
  • 2021-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多