JSONファイルを使用してユーザーをJavaのAzureB2C ActiveDirectoryに一括アップロードする

Aug 24 2020

JavaコードをAzureB2C Active Directoryに接続し、使用しました

User user = graphClient.users("611us0f2a-7608-4edd-8c4c-7871d8e70b8e").buildRequest().get();

get()リクエストで結果が得られました。これは、接続が正しいことを意味します。ローカルデータベースに存在するユーザーを一括アップロードしたいので、JSON形式のファイル(user_data.json)を作成しました。これは次のようになります-

{
  "users": [
    {
      "displayName": "Amanda Polly",
      "givenName": "Amanda",
      "surname": "Polly",
      "extension_user_type": "user",
      "identities": [
        {
          "signInType": "emailAddress",
          "issuerAssignedId": "[email protected]"
        }
      ],
      "extension_timezone": "PST",
      "extension_locale": "en-US",
      "extension_tenant": "EG1234"
    },
    {
      "displayName": "Lowa Doe",
      "givenName": "Lowa",
      "surname": "Doe",
      "extension_user_type": "user",
      "identities": [
        {
          "signInType": "userName",
          "issuerAssignedId": "lowadow123"
        }
      ],
      "extension_timezone": "PST",
      "extension_locale": "en-US",
      "extension_tenant": "EG1234"
    }
   ]
}

ここJSONには、ラッパー「users」があり、その後にすべてのユーザー(JSONで表示されている場合は現在2人のユーザー)が続くので、mircosoftグラフAPIを使用して作成する必要があります。どうすればよいですか?私が知っていることは、各ユーザーをオブジェクトに変換し、post()を使用して送信してディレクトリに作成する必要があることです。

それを行う方法の解決策を提案してください。

||あなたが提案したように、コードが来たときにアレンを読むために、

private static void updateExtension(String userID, IGraphServiceClient graphClient) 
    {
        User extensionForUser = new User();
        extensionForUser.additionalDataManager().put("extension_dfa4f1a9a2f94cd1bf8826c50d4d0464_user_type", new JsonPrimitive(extension_user_type));
        extensionForUser.additionalDataManager().put("extension_dfa4f1a9a2f94cd1bf8826c50d4d0464_timezone", new JsonPrimitive(extension_timezone));
        extensionForUser.additionalDataManager().put("extension_dfa4f1a9a2f94cd1bf8826c50d4d0464_extension_locale", new JsonPrimitive(extension_locale));
        extensionForUser.additionalDataManager().put("extension_dfa4f1a9a2f94cd1bf8826c50d4d0464_tenant", new JsonPrimitive(extension_tenant));
        graphClient.users(userID)
                .buildRequest()
                .patch(extensionForUser);
        
    }

.patch(extensionForUser)->のようなエラーで壊れています

Error message: Resource 'null' does not exist or one of its queried reference-property objects are not present.

PATCH https://graph.microsoft.com/v1.0/users/null
SdkVersion : graph-java/v1.9.0
Authorization : Bearer eyJ0eXAiOiJKV1QiLCJub25jZSI[...]
{"extension_dfa4f1a9a2f94cd1bf8826c50d4d0464_tenan[...]

404 : Not Found
[...]

しかし、Azure Directoryを確認すると、ユーザーが作成されました。コードを再実行したときに、同じプリンシパル名のユーザーがすでに存在していると表示されたためです。

また、最初のイテレーションで中断しているため、この背後で作成されるユーザーは作成されません。

アップデート2:

アップデート3:私が試したことの説明。

userID = userCreateCall(displayName, givenName, surname, extension_user_type, extension_timezone, extension_locale, extension_tenant, signInType, issuerAssignedId, graphClient);
                System.out.println(userID);
                updateExtension(userID, graphClient, extension_user_type, extension_timezone, extension_locale, extension_tenant);

すべてのユーザーを繰り返し処理するforループがある場合は、すべてのユーザーに対して[displayName、givenName、surname、extension_user_type、extension_timezone、extension_locale、extension_tenant、signInType、issuerAssignedId]を選択し、パラメーターとして渡します。userCreateCall()メソッドあなたは上記のコードで見ることができるよう、私はまた、PARAM内graphClientを渡しています。

これは私のuserCreateCall()メソッドです。

User createNewUser = new User();
        createNewUser.displayName = displayName;
        createNewUser.givenName = givenName;
        createNewUser.surname = surname;
        
        LinkedList<ObjectIdentity> identitiesList = new LinkedList<ObjectIdentity>();
        ObjectIdentity identities = new ObjectIdentity();
        identities.signInType = signInType;
        identities.issuerAssignedId = issuerAssignedId;
        identities.issuer = "demoUserEngine.onmicrosoft.com";
        identitiesList.add(identities);
        
        createNewUser.identities = identitiesList;
        
        PasswordProfile passwordProfile = new PasswordProfile();
        passwordProfile.password = "passwordPASSWORD!";
        passwordProfile.forceChangePasswordNextSignIn = false;
        
        createNewUser.passwordProfile = passwordProfile;
        createNewUser.passwordPolicies = "DisablePasswordExpiration";
        
        User buildUserRequest = graphClient.users()
                                .buildRequest()
                                .post(createNewUser);
        
        
        return createNewUser.id;

createNewUser.idを返しますが、ここではNULLになると思います。したがって、

userID = userCreateCall(displayName, givenName, surname, extension_user_type, extension_timezone, extension_locale, extension_tenant, signInType, issuerAssignedId, graphClient);
System.out.println(userID); // THIS IS giving NULL

したがって、update updateExtensionuserIDを呼び出すと、NULLになります。

updateExtensionメソッドにコメントを付けて、userIDを使用する必要がなく、コードが壊れないようにしました。ユーザー作成の例外はなく、userIDだけがNULLになります。

回答

1 AllenWu Aug 25 2020 at 13:42

拡張属性を使用してAzureB2Cでユーザーを直接作成することはできません。

最初に通常の属性でユーザーを作成してから、ユーザーの拡張属性を更新する必要があります。

(あなたが拡張属性を作成する必要がまず第一にextension_user_typeextension_timezoneextension_localeextension_tenant以下による)のAzureポータルでカスタム属性を作成します。あなただけ入力して注意してくださいuser_typetimezonelocaletenant拡張属性の名前として。

次に、次の形式で拡張属性を生成します。extension_ApplicationClientID_attributenameここで、ApplicationClientIDは、アプリケーションのアプリケーション(クライアント)IDb2c-extensions-appです([アプリの登録] > [ Azureポータルのすべてのアプリケーション]にあります)。

拡張属性名で表されるアプリケーション(クライアント)IDには、ハイフンが含まれていないことに注意してください。たとえば、アプリケーションのアプリケーションIDはb2c-extensions-appですa554ad42-83c8-475e-a9aa-ac09a9h25c67。ただし、実際の拡張属性はextension_a554ad4283c8475ea9aaac09a9h25c67_attributenameであり、すべてが削除されてい-ます。こちらのリファレンスを参照してください。

現在、ユーザーを作成する準備をしています。私のコードを参照してください:

public static void main(String[] args) {
        List<String> scopes = Arrays.asList("https://graph.microsoft.com/.default");
        String clientId = "3******9-5a11-4823-88de-13*******cad";
        String clientSecret = "**********************************";
        String tenant = "allentest001.onmicrosoft.com";
        ClientCredentialProvider authProvider = new ClientCredentialProvider(
                clientId,
                scopes,
                clientSecret,
                tenant,
                NationalCloud.Global);

        IGraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider).buildClient();
        String userId = createUser(graphClient);
        updateExtension(userId, graphClient);
    }

    private static String createUser(IGraphServiceClient graphClient) {
        User user = new User();
        user.displayName = "Amanda Polly";
        user.givenName = "Amanda";
        user.surname = "Polly";
        LinkedList<ObjectIdentity> identitiesList = new LinkedList<ObjectIdentity>();
        ObjectIdentity identities = new ObjectIdentity();
        identities.signInType = "emailAddress";
        identities.issuer = "allentest001.onmicrosoft.com";
        identities.issuerAssignedId = "[email protected]";
        identitiesList.add(identities);
        user.identities = identitiesList;
        PasswordProfile passwordProfile = new PasswordProfile();
        passwordProfile.password = "{your password here}";
        passwordProfile.forceChangePasswordNextSignIn = false;
        user.passwordProfile = passwordProfile;
        user.passwordPolicies = "DisablePasswordExpiration";

        User newUser = graphClient.users()
                .buildRequest()
                .post(user);

        return newUser.id;
    }


    private static void updateExtension(String userId, IGraphServiceClient graphClient) {
        User user = new User();
        user.additionalDataManager().put("extension_a554ad4283c8475ea9aaac09a9h25c67_user_type", new JsonPrimitive("user"));
        user.additionalDataManager().put("extension_a554ad4283c8475ea9aaac09a9h25c67_timezone", new JsonPrimitive("PST"));
        user.additionalDataManager().put("extension_a554ad4283c8475ea9aaac09a9h25c67_extension_locale", new JsonPrimitive("PST"));
        user.additionalDataManager().put("extension_a554ad4283c8475ea9aaac09a9h25c67_tenant", new JsonPrimitive("EG1234"));
        graphClient.users(userId)
                .buildRequest()
                .patch(user);
    }

ユーザーの作成時にパスワードを設定する必要があることに注意してください。(私のコードの詳細を参照してください)

krishg Aug 24 2020 at 22:41

一度に1人のユーザーを作成するには、Javaで各ユーザーを呼び出す必要があります。そのためにAPIドキュメントを参照してくださいここに。ただし、GraphSDKはバッチ処理もサポートしています。個々のリクエストで構成されるJSONペイロードを作成し、$ batchエンドポイントへの単一のPOSTを作成できます。詳しくはこちらをご覧ください。

注:Microsoft GraphでのJSONバッチ処理の現在の制限については、既知の問題を参照してください。