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) 범위는 로그인 유형

    실행 결과



    이 작업을 수행하면 Azure AD B2C에서 지정한 특성의 사용자가 만들어졌는지 확인할 수 있습니다.
    htps // g 등 ph. 미 c 로소 ft. 이 m/. 푹신한 lt

    다음 번에는 사용자 정보를 업데이트하는 방법을 설명합니다.
    읽어 주셔서 감사합니다.

    검증 코드



    Graph API 참조

    좋은 웹페이지 즐겨찾기