적용 대상: 흰색 확인 표시가 있는 녹색 원 Workforce 테넌트, 흰색 확인 표시가 있는 녹색 원 외부 테넌트 (자세히 알아보기)
이 자습서는 Angular SPA(단일 페이지 애플리케이션)를 빌드하고 Microsoft ID 플랫폼을 사용하여 인증을 추가하는 방법을 보여 주는 시리즈의 마지막 부분입니다. 이 시리즈 2부에서는 Angular SPA를 만들고 직원 테넌트와의 인증을 위해 준비했습니다.
이 자습서에서는
- Angular 애플리케이션에 데이터 처리를 추가합니다.
- 애플리케이션을 테스트하고 사용자 데이터를 추출합니다.
필수 구성 요소
애플리케이션 UI에서 볼 데이터 추출
Microsoft Graph API와 상호 작용하도록 Angular 애플리케이션을 구성하려면 다음 단계를 완료합니다.
src/app/profile/profile.component.ts
파일을 열고 내용을 다음 코드 조각으로 바꿉니다.// Required for Angular import { Component, OnInit } from '@angular/core'; // Required for the HTTP GET request to Graph import { HttpClient } from '@angular/common/http'; type ProfileType = { businessPhones?: string, displayName?: string, givenName?: string, jobTitle?: string, mail?: string, mobilePhone?: string, officeLocation?: string, preferredLanguage?: string, surname?: string, userPrincipalName?: string, id?: string } @Component({ selector: 'app-profile', templateUrl: './profile.component.html' }) export class ProfileComponent implements OnInit { profile!: ProfileType; tokenExpiration!: string; constructor( private http: HttpClient ) { } // When the page loads, perform an HTTP GET request from the Graph /me endpoint ngOnInit() { this.http.get('https://graph.microsoft.com/v1.0/me') .subscribe(profile => { this.profile = profile; }); this.tokenExpiration = localStorage.getItem('tokenExpiration')!; } }
Angular의
ProfileComponent
Microsoft Graph의/me
엔드포인트에서 사용자 프로필 데이터를 가져옵니다.ProfileType
및displayName
같은 속성을 구조화하기 위한mail
정의합니다.ngOnInit
에서HttpClient
을 사용하여 GET 요청을 보내고 응답을profile
에 할당합니다. 또한localStorage
tokenExpiration
토큰 만료 시간을 검색하고 저장합니다.src/app/profile/profile.component.html
파일을 열고 내용을 다음 코드 조각으로 바꿉니다.<div class="profile"> <p><strong>Business Phones:</strong> {{profile?.businessPhones}}</p> <p><strong>Display Name:</strong> {{profile?.displayName}}</p> <p><strong>Given Name:</strong> {{profile?.givenName}}</p> <p><strong>Job Title:</strong> {{profile?.jobTitle}}</p> <p><strong>Mail:</strong> {{profile?.mail}}</p> <p><strong>Mobile Phone:</strong> {{profile?.mobilePhone}}</p> <p><strong>Office Location:</strong> {{profile?.officeLocation}}</p> <p><strong>Preferred Language:</strong> {{profile?.preferredLanguage}}</p> <p><strong>Surname:</strong> {{profile?.surname}}</p> <p><strong>User Principal Name:</strong> {{profile?.userPrincipalName}}</p> <p><strong>Profile Id:</strong> {{profile?.id}}</p> <br><br> <p><strong>Token Expiration:</strong> {{tokenExpiration}}</p> <br><br> <p>Refreshing this page will continue to use the cached access token until it nears expiration, at which point a new access token will be requested.</p> </div>
이 코드는 Angular의 보간 구문을 사용하여
profile
개체(예:businessPhones
,displayName
,jobTitle
)의 속성을 바인딩하는 사용자 프로필 정보를 표시하는 HTML 템플릿을 정의합니다. 또한tokenExpiration
값을 표시하고 페이지를 새로 고치면 만료가 가까워질 때까지 캐시된 액세스 토큰이 사용되며 그 후에 새 토큰이 요청된다는 메모가 포함되어 있습니다.
애플리케이션 테스트
애플리케이션이 작동하려면 Angular 애플리케이션을 실행하고 로그인하여 Microsoft Entra 테넌트로 인증하고 사용자 데이터를 추출해야 합니다.
애플리케이션을 테스트하려면 다음 단계를 완료합니다.
터미널에서 다음 명령을 실행하여 Angular 애플리케이션을 실행합니다.
ng serve --open
로그인 단추를 선택하여 Microsoft Entra 테넌트로 인증합니다.
로그인한 후 프로필 보기 링크를 선택하여 프로필 페이지로 이동합니다. 사용자 이름, 전자 메일, 직함 및 기타 세부 정보를 포함하여 사용자 프로필 정보가 표시되는지 확인합니다.
로그아웃 단추를 선택하여 애플리케이션에서 로그아웃합니다.
다음 단계
웹 API를 빌드하는 방법에 대한 다음 자습서 시리즈를 시도하여 Microsoft ID 플랫폼을 사용하는 방법을 알아봅니다.
자습서: Microsoft ID 플랫폼 웹 API 등록