Microsoft Graph API を利用した Azure AD B2C のユーザー操作(作成編)


Microsoft Azure 上でクラウドネイティブなシステムを作る際は、ユーザー情報管理や認証処理を Azure AD B2C を利用して実現することが一般的です。
本記事では、Microsoft Graph API を利用して Azure AD B2C 上のユーザー情報を作成、更新、削除する方法を紹介します。

前提条件

  • Azure AD B2C テナントが作成済みであること
  • クライアント側は Spring-Boot ベースの Java アプリケーション

Microsoft Graph アプリケーションを登録する

Microsoft Graph API の実行には、まず Microsoft Graph アプリケーションを Azure AD B2C テナントに登録します。
以下の公開情報を参考に実施してください。
Microsoft Graph アプリケーションを登録する

pom.xml

pom.xml
        <dependency>
            <groupId>com.microsoft.graph</groupId>
            <artifactId>microsoft-graph</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.microsoft.graph</groupId>
            <artifactId>microsoft-graph-auth</artifactId>
            <version>0.2.0</version>
        </dependency>

Azure AD B2C ユーザーの作成

TestGraphApi.java
    @Test
    void createUser() {

        /**
         * ユーザーインスタンスの作成
         */
        User user = new User();

        /**
         * パスワード属性の設定
         */
        PasswordProfile passwordProfile = new PasswordProfile();
        passwordProfile.password = PASSWORD;
        passwordProfile.forceChangePasswordNextSignIn = false;  //(*1)
        user.passwordProfile = passwordProfile;

        /**
         * ユーザー属性の設定(必須項目のみ)
         */
        user.displayName = DISPLAY_NAME;
        user.accountEnabled = true;
        user.mailNickname = NICKNAME;
        user.userPrincipalName = USER_PRINCIPAL_NAME; //(*2)
        ObjectIdentity objectIdentity = new ObjectIdentity();
        objectIdentity.signInType = "userName";  //(*3)
        objectIdentity.issuer = ISSUER;  //(*4)
        objectIdentity.issuerAssignedId = ISSUER_ASSIGNED_ID;
        List<ObjectIdentity> identities = new ArrayList();
        identities.add(objectIdentity);
        user.identities = identities;

        /**
         * Azure AD B2C 上にユーザーを作成
         */
        User response =
                createIGraphServiceClient(
                        CLIENT_ID,
                        CLIENT_SECRET,
                        DOMAIN_NAME)
                        .users()
                        .buildRequest()
                        .post(user); //(*5)

    }

(*1) 初回サインイン時の強制パスワード変更の有無
(*2) ユーザープリンシパル名は、{任意の文字列}@{テナント名} onmicrosoft.com にする
(*3) ユーザー名認証(username)、メールアドレス認証(emailAddress)、フェデレーション連携認証(federated)を選択可能 サインインタイプ
(*4) {テナント名} onmicrosoft.com を指定する
(*5) DOMAIN_NAME は、{テナント名} onmicrosoft.com を指定する

createIGraphServiceClient
    private IGraphServiceClient createIGraphServiceClient(
            String clientId, String clientSecret, String tenantName) {

        List<String> scopes = new ArrayList();
        scopes.add(SCOPE);
        ClientCredentialProvider authProvider = new ClientCredentialProvider(clientId,
                scopes, clientSecret, tenantName, NationalCloud.Global);
        IGraphServiceClient graphClient = GraphServiceClient
                .builder()
                .authenticationProvider(authProvider)
                .buildClient();
        return graphClient;
    }

(*6) スコープは、https://graph.microsoft.com/.default を指定する

実行結果

上記を実行すると、Azure AD B2C 上に指定した属性のユーザーが作成されていることが確認できます。

次回は、ユーザー情報の更新方法について記述します。
読んで頂きありがとうございました。

検証コード

Graph API リファレンス