Learn how to build a native app signing-in users authenticated by AD FS 2019 and acquiring tokens using MSAL library to call web APIs.
이 문서를 읽기 전에 AD FS 개념 및 권한 부여 코드 부여 흐름에 대해 잘 알고 있어야 합니다.
Overview
이 흐름에서는 네이티브 앱(공용 클라이언트)에 인증을 추가하므로 사용자를 로그인하여 Web API를 호출할 수 있습니다. To call a Web API from a Native App that signs in users, you can use MSAL's AcquireTokenInteractive token acquisition method. 이 상호 작용을 사용하도록 설정하기 위해 MSAL이 웹 브라우저를 활용합니다.
To better understand how to configure a Native App in AD FS to acquire access token interactively, let's use a sample available here and walkthrough the app registration and code configuration steps.
Pre-requisites
- GitHub 클라이언트 도구
- AD FS 2019 이상 구성 및 실행
- Visual Studio 2013 이상
AD FS에 앱 등록
이 섹션은 네이티브 앱을 공용 클라이언트로 등록하고 Web API를 AD FS에서 신뢰 당사자(RP)로 등록하는 방법을 설명합니다.
AD FS 관리에서 애플리케이션 그룹을 마우스 오른쪽 단추로 클릭하 고 애플리케이션 그룹 추가를 선택합니다.
On the Application Group Wizard, for the Name enter NativeAppToWebApi and under Client-Server applications select the Native application accessing a Web API template. Click Next.
Copy the Client Identifier value. It will be used later as the value for ClientId in the application's App.config file. Enter the following for Redirect URI:https://ToDoListClient. Click Add. Click Next.
On the Configure Web API screen, enter the Identifier:https://localhost:44321/. Click Add. Click Next. This value will be used later in the application's App.config and Web.config files.
On the Apply Access Control Policy screen, select Permit everyone and click Next.
On the Configure Application Permissions screen, make sure openid is selected and click Next.
On the Summary screen, click Next.
On the Complete screen, click Close.
In AD FS Management, click on Application Groups and select NativeAppToWebApi application group. Right-click and select Properties.
NativeAppToWebApi 속성 화면에서 Web API의 NativeAppToWebApi – Web API를 선택하고 편집…을 클릭
NativeAppToWebApi – Web API 속성 화면에서 발급 변환 규칙 탭을 선택하고 규칙 추가…를 클릭합니다.
클레임 규칙 추가 마법사의 클레임 규칙 템플릿: 드롭다운에서 들어오는 클레임 변환을 선택하고 다음을 클릭합니다.
Enter NameID in Claim rule name: field. Select Name for Incoming claim type:, Name ID for Outgoing claim type: and Common Name for Outgoing name ID format:. click Finish.
NativeAppToWebApi – Web API 속성 화면에서 확인을 클릭하고 NativeAppToWebApi 속성 화면을 클릭합니다.
Code Configuration
이 섹션은 사용자를 로그인하도록 네이티브 앱을 구성하고 Web API를 호출하기 위한 토큰을 검색하는 방법을 설명합니다.
Download the sample from here
Visual Studio를 사용하여 샘플 열기
App.config 파일을 엽니다. 다음을 수정합니다.
ida:Authority - 'h' 입력
ttps://[your AD FS hostname]/adfs
ida:ClientId - enter the Client Identifier value from #3 in App Registration in AD FS section above.
ida:RedirectUri - enter the Redirect URI value from #3 in App Registration in AD FS section above.
todo:TodoListResourceId – enter the Identifier value from #4 in App Registration in AD FS section above
ida: todo:TodoListBaseAddress - enter the Identifier value from #4 in App Registration in AD FS section above.
Web.config 파일을 엽니다. 다음을 수정합니다.
ida:Audience - enter the Identifier value from #4 in App Registration in AD FS section above
ida: AdfsMetadataEndpoint -
https://[your AD FS hostname]/federationmetadata/2007-06/federationmetadata.xml
을 입력하세요.
샘플 테스트
이 섹션은 위에서 구성한 샘플을 테스트하는 방법을 보여줍니다.
코드가 변경되면 솔루션을 다시 빌드
Visual Studio에서 솔루션을 마우스 오른쪽 단추로 클릭하고 시작 프로젝트 설정... 선택
On the Properties pages make sure Action is set to Start for each of the Projects
Visual Studio 맨 위에서 녹색 화살표를 클릭합니다.
On the Native App's Main screen, click on Sign In.
네이티브 앱 화면이 표시되지 않으면 시스템에 프로젝트 리포지토리가 저장된 폴더에서 *msalcache.bin
파일을 검색하여 제거합니다.
AD FS 로그인 페이지로 리디렉션될 수 있습니다. 로그인하세요.
로그인한 후 Native App을 Web Api로 빌드라는 텍스트를 할 일 만들기 항목에 입력합니다. Click Add item. 그러면 할 일 목록 서비스(Web API)가 호출되고 해당 항목이 캐시에 추가됩니다.