node.js - Azure 图形 API 在 Node JS 中使用自定义用户属性创建 B2C 用户

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

您能否帮助我使用 Node js 客户端在 Azure AD B2C 中创建用户。

在该请求中,我需要填充“signInNames”和我在 B2c 中为应用程序创建的自定义用户属性。

如果您分享示例请求,我们将不胜感激。

最佳答案

以下代码使用 Azure Active Directory Authentication Library (ADAL) for Node.jsrequest与之交互的包 the Azure AD Graph API .

1) 获取用于 Azure AD Graph API 的访问 token :

const AuthenticationContext = require("adal-node").AuthenticationContext;

const tenant = "myb2cdomain.onmicrosoft.com";
const authority = `https://login.microsoftonline.com/{tenant}`;

const authenticationContext = new AuthenticationContext(authority);

function acquireTokenForApplication(clientId, clientSecret, callback) {
    authenticationContext.acquireTokenWithClientCredentials("https://graph.windows.net/", clientId, clientSecret, function(err, tokenResponse) {
        if (err) {
            callback(err);
            return;
        }

        callback(null, tokenResponse.access_token);
    });
}

2)创建用户对象:

const userToBeCreated = {
    accountEnabled: true,
    creationType: "LocalAccount",
    displayName: "Alex Wu",
    passwordPolicies: "DisablePasswordExpiration",
    passwordProfile: {
        forceChangePasswordNextLogin: false,
        password: "Test1234"
    },
    signInNames: [
        {
            type: "emailAddress",
            value: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2849444d505f684d50494558444d064b4745" rel="noreferrer noopener nofollow">[email protected]</a>"
        }
    ],
    "extension_xxx_<customAttributeName>": <customAttributeValue>
};

其中“xxx”必须替换为 b2c-extensions-app 应用程序的应用程序标识符(不带连字符)。

例如:

"extension_ab603c56068041afb2f6832e2a17e237_SkypeId": "alexw.skype"

3) 将用户对象发送至the Azure AD Graph API :

function createUser(tenantId, accessToken, userToBeCreated, callback) {
    request.post({
        url: `https://graph.windows.net/${encodeURIComponent(tenantId)}/users?api-version=1.6`,
        auth: {
            bearer: accessToken
        },
        body: userToBeCreated,
        json: true
    }, (err, response, responseBody) => {
        if (err) {
            callback(err);
            return;
        }

        if (!isSuccessStatusCode(response.statusCode)) {
            const errorResult = responseBody;

            callback({
                code: errorResult["odata.error"].code,
                message: errorResult["odata.error"].message.value
            });

            return;
        }

        const createdUser = responseBody;
        callback(null, createdUser);
    });
}

关于node.js - Azure 图形 API 在 Node JS 中使用自定义用户属性创建 B2C 用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49965121/

相关文章:

node.js - Mongoose - 获取模型中数组的长度

sql-server - 许多更新出现死锁错误

javascript - 为什么要在 http.createServer() 中返回一个值

node.js - AWS Lambda 不适用于 `async` ,仅适用于回调?

ruby-on-rails - 生成和发布基于 Ruby 的 REST API 文档

java - 将 @DefaultValue 与 RESTEasy 客户端框架结合使用

ios - 如何只允许我的移动应用程序请求我的 API?

asp.net - .Net Application Insights 2.5 不跟踪 HTTP 请求

azure - 是否可以在 Azure 容器应用程序中创建一次性(运行至完成)作业?

azure - HDInsight 模拟器未在 Windows 上运行/连接异常