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

Din*_*sha 3 rest azure node.js azure-ad-graph-api azure-ad-b2c

你能帮我在Azure AD B2C中使用node js client创建一个用户吗?

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

如果您共享样本请求,我们非常感谢.

Chr*_*ett 5

以下代码对Node.js使用Azure Active Directory身份验证库(ADAL),请求包与Azure AD Graph API进行交互.

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

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);
    });
}
Run Code Online (Sandbox Code Playgroud)

2)创建用户对象:

const userToBeCreated = {
    accountEnabled: true,
    creationType: "LocalAccount",
    displayName: "Alex Wu",
    passwordPolicies: "DisablePasswordExpiration",
    passwordProfile: {
        forceChangePasswordNextLogin: false,
        password: "Test1234"
    },
    signInNames: [
        {
            type: "emailAddress",
            value: "alexw@example.com"
        }
    ],
    "extension_xxx_<customAttributeName>": <customAttributeValue>
};
Run Code Online (Sandbox Code Playgroud)

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

例如:

"extension_ab603c56068041afb2f6832e2a17e237_SkypeId": "alexw.skype"
Run Code Online (Sandbox Code Playgroud)

3)将用户对象发送到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);
    });
}
Run Code Online (Sandbox Code Playgroud)

  • 它是[b2c-extensions-app](https://docs.microsoft.com/en-us/azure/active-directory-b2c/active-directory-b2c-reference-extensions-app)应用程序创建时Azure AD B2C租户已创建. (2认同)