LoginSignup
2
3

More than 3 years have passed since last update.

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

Last updated at Posted at 2021-04-28

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 上に指定した属性のユーザーが作成されていることが確認できます。
スクリーンショット 2021-05-01 0.58.49.png

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

検証コード

Graph API リファレンス

2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3