Share via


시나리오: Web API를 호출하는 Web API(시나리오 대신)

적용 대상: Windows Server 2022, Windows Server 2019, AD FS 2019 이상

사용자를 대신하여 다른 Web API를 호출하는 Web API를 빌드하는 방법을 알아봅니다.

이 문서를 읽기 전에 AD FS 개념온-Behalf_Of 흐름에 대해 잘 알고 있어야 합니다.

개요

  • 아래 다이어그램에 표시되지 않는 클라이언트(웹앱)는 보호된 Web API를 호출하고 "권한 부여" Http 헤더에 JWT 전달자 토큰을 제공합니다.

  • 보호된 Web API는 토큰의 유효성을 검사하고 MSAL AcquireTokenOnBehalfOf 메서드를 사용하여 AD FS에서 다른 토큰을 요청하여 사용자를 대신하여 두 번째 웹 API(다운스트림 웹 API라는 이름)를 호출할 수 있도록 합니다.

  • 보호된 웹 API는 이 토큰을 사용하여 다운스트림 API를 호출합니다. AcquireTokenSilentlater를 호출하여 다른 다운스트림 API에 대한 토큰을 요청할 수도 있습니다(하지만 여전히 동일한 사용자를 대신하여). AcquireTokenSilent는 필요할 때 토큰을 새로 고칩니다.

    overview

AD FS에서 인증 시나리오를 대신하여 구성하는 방법을 더 잘 이해하려면 여기에서 사용할 수 있는 샘플을 사용하고 앱 등록 및 코드 구성 단계를 연습해 보겠습니다.

필수 구성 요소

  • GitHub 클라이언트 도구
  • AD FS 2019 이상 구성 및 실행
  • Visual Studio 2013 이상

AD FS에서 앱 등록

이 섹션에서는 네이티브 앱을 공용 클라이언트로 등록하고 Web API를 AD FS에서 RP(신뢰 당사자)로 등록하는 방법을 보여 줍니다.

  1. AD FS 관리에서 애플리케이션 그룹을 마우스 오른쪽 단추로 클릭하고 애플리케이션 그룹 추가를 선택합니다.

  2. 애플리케이션 그룹 마법사에서 이름에 WebApiToWebApi를 입력하고 클라이언트 서버 애플리케이션에서 Web API 템플릿에 액세스하는 네이티브 애플리케이션을 선택합니다. 다음을 클릭합니다.

    Screenshot of the Welcome page of the Add Application Group Wizard showing the Native application accessing a Web API template highlighted.

  3. 복사는 클라이언트 식별자 값입니다. 나중에 애플리케이션의 App.config 파일에서 ClientId의 값으로 사용됩니다. 리디렉션 URI에 다음을 입력합니다. -https://ToDoListClient. 추가를 클릭합니다. 다음을 클릭합니다.

    Screenshot of the Native application page of the Add Application Group Wizard showing the redirect U R I .

  4. 웹 API 구성 화면에서 식별자를 입력합니다.https://localhost:44321/. 추가를 클릭합니다. 다음을 클릭합니다. 이 값은 애플리케이션의 App.configWeb.Config 파일에서 나중에 사용됩니다.

    Screenshot of the Configure Web API page of the Add Application Group Wizard showing the correct identifier.

  5. 액세스 제어 정책 적용 화면에서 모든 사용자 허용을 선택하고 다음을 클릭합니다.

    Screenshot of the Choose Access Control Policy page of the Add Application Group Wizard showing the Permit everyone option highlighted.

  6. 애플리케이션 사용 권한 구성 화면에서 openid선택하고 user_impersonation. 다음을 클릭합니다.

    Screenshot of the Configure Application Permissions page of the Add Application Group Wizard showing open I D selected.

  7. 요약 화면에서 다음을 클릭합니다.

  8. 전체 화면에서 닫기를 클릭합니다.

  9. AD FS 관리에서 애플리케이션 그룹을 클릭하고 WebApiToWebApi 애플리케이션 그룹을 선택합니다. 마우스 오른쪽 단추를 클릭하고 속성을 선택합니다.

    Screenshot of the A D F S Management dialog box showing the WebApiToWebApi group highlighted and the Properties option in the dropdown list.

  10. WebApiToWebApi 속성 화면에서 애플리케이션 추가...를 클릭합니다.

    Screenshot of the WebApiToWebApi Properties dialog box showing the WebApiToWebApi - Web A P I application listed.

  11. 독립 실행형 애플리케이션에서 서버 애플리케이션을 선택합니다.

    Screenshot of the Welcome page of the Add a new application to WebApiToWebApi wizard showing the Server application option highlighted.

  12. 서버 애플리케이션 화면에서 클라이언트 식별자리디렉션 URI추가 https://localhost:44321/ 합니다.

    Screenshot of the Server application page of the Add a new application to WebApiToWebApi wizard showing the correct client identifier and redirect U R I.

  13. 애플리케이션 자격 증명 구성 화면에서 공유 비밀 생성을 선택합니다. 나중에 사용할 수 있는 비밀을 복사합니다.

    Screenshot of the Configure Application Credentials application page of the Add a new application to WebApiToWebApi wizard showing the Generate a shared secret option selected and the generated shared secret highlighted.

  14. 요약 화면에서 다음을 클릭합니다.

  15. 전체 화면에서 닫기를 클릭합니다.

  16. AD FS 관리에서 애플리케이션 그룹을 클릭하고 WebApiToWebApi 애플리케이션 그룹을 선택합니다. 마우스 오른쪽 단추를 클릭하고 속성을 선택합니다.

    Second screenshot of the A D F S Management dialog box showing the WebApiToWebApi group highlighted and the Properties option in the dropdown list.

  17. WebApiToWebApi 속성 화면에서 애플리케이션 추가...를 클릭합니다.

    Second screenshot of the WebApiToWebApi Properties dialog box showing the WebApiToWebApi - Web A P I application listed.

  18. 독립 실행형 애플리케이션에서 Web API를 선택합니다.

    Screenshot of the Welcome page of the Add a new application to WebApiToWebApi wizard showing the Web A P I option highlighted.

  19. Web API 구성에서 식별자로 추가 https://localhost:44300 합니다.

    Screenshot of the Configure Web A P I page of the Add a new application to WebApiToWebApi wizard showing the correct redirect U R I.

  20. 액세스 제어 정책 적용 화면에서 모든 사용자 허용을 선택하고 다음을 클릭합니다.

    Screenshot of the Choose Access Control Policy page of the Add a new application to WebApiToWebApi wizard showing the Permit everyone option highlighted.

  21. 애플리케이션 사용 권한 구성 화면에서 다음을 클릭합니다.

    Screenshot of the Configure Application Permissions page of the Add a new application to WebApiToWebApi wizard showing the Next option called out.

  22. 요약 화면에서 다음을 클릭합니다.

  23. 전체 화면에서 닫기를 클릭합니다.

  24. WebApiToWebApi – Web API 2 속성 화면에서 확인을 클릭합니다.

  25. WebApiToWebApi 속성 화면에서 WebApiToWebApi – Web API를 선택하고 편집...을 클릭합니다.

    Screenshot of the WebApiToWebApi Properties dialog box showing the WebApiToWebApi - Web A P I application highlighted.

  26. WebApiToWebApi – Web API 속성 화면에서 발급 변환 규칙 탭을 선택하고 규칙 추가...를 클릭합니다.

    Screenshot of the WebApiToWebApi - Web A P I Properties dialog box showing the Issuance Transform Rules tab.

  27. 변환 클레임 규칙 추가 마법사의 드롭다운에서 사용자 지정 규칙을 사용하여 클레임 보내기를 선택하고 다음을 클릭합니다.

    Screenshot of the Select Rule Template page of the Add Transform Claim Rule Wizard showing the Send Claims Using a Custom Rule option selected.

  28. 클레임 규칙 이름에 PassAllClaims 입력합니다. field 및 x:[] => issue(claim=x); 사용자 지정 규칙: 필드에 클레임 규칙을 입력하고 마침을 클릭합니다.

    Screenshot of the Configure Rule page of the Add Transform Claim Rule Wizard showing the configuration explained above.

  29. WebApiToWebApi – Web API 속성 화면에서 확인을 클릭합니다.

  30. WebApiToWebApi 속성 화면에서 WebApiToWebApi – Web API 2를 선택하고 편집...을 클릭합니다.
    Screenshot of the WebApiToWebApi Properties dialog box showing the WebApiToWebApi - Web A P I 2 application highlighted.

  31. WebApiToWebApi – Web API 2 속성 화면에서 발급 변환 규칙 탭을 선택하고 규칙 추가...

  32. 변환 클레임 규칙 추가 마법사의 드롭다운에서 사용자 지정 규칙을 사용하여 클레임 보내기를 선택하고 다음을 클릭합니다. Second screenshot of the Select Rule Template page of the Add Transform Claim Rule Wizard showing the Send Claims Using a Custom Rule option selected.

  33. 클레임 규칙 이름에 PassAllClaims를 입력합니다. field 및 x:[] => issue(claim=x); 사용자 지정 규칙: 필드에 클레임 규칙을 입력하고 마침을 클릭합니다.

    Second screenshot of the Configure Rule page of the Add Transform Claim Rule Wizard showing the configuration explained above.

  34. WebApiToWebApi – Web API 2 속성 화면에서 확인을 클릭한 다음 WebApiToWebApi 속성 화면에서 확인을 클릭합니다.

코드 구성

이 섹션에서는 다른 Web API를 호출하도록 Web API를 구성하는 방법을 보여줍니다.

  1. 여기에서 샘플 다운로드

  2. Visual Studio를 사용하여 샘플 열기

  3. App.config 파일을 엽니다. 다음을 수정합니다.

    • ida:Authority - https://[AD FS 호스트 이름]/adfs/를 입력합니다.

    • ida:ClientId - 위의 AD FS 섹션에서 앱 등록에서 #3의 값을 입력합니다.

    • ida:RedirectUri - 위의 AD FS 섹션에서 앱 등록에서 #3의 값을 입력합니다.

    • todo:TodoListResourceId – 위의 AD FS 섹션에서 앱 등록에서 #4의 식별자 값을 입력합니다.

    • ida: todo:TodoListBaseAddress - 위의 AD FS 섹션에서 앱 등록에서 #4의 식별자 값을 입력합니다.

      Screenshot of the App config file showing the modified values.

  4. ToDoListService에서 Web.config 파일을 엽니다. 다음을 수정합니다.

    • ida:Audience - 위의 AD FS 섹션에서 앱 등록에서 #12의 클라이언트 식별자 값을 입력합니다.

    • ida:ClientId - 위의 AD FS 섹션에서 앱 등록에서 #12의 클라이언트 식별자 값을 입력합니다.

    • Ida: ClientSecret - 위의 AD FS 섹션에서 앱 등록에서 #13에서 복사한 공유 비밀을 입력합니다.

    • ida:RedirectUri - 위의 AD FS 섹션에서 앱 등록에서 #12의 RedirectUri 값을 입력합니다.

    • ida: AdfsMetadataEndpoint - https://[AD FS 호스트 이름]/federationmetadata/2007-06/federationmetadata.xml 입력

    • ida:OBOWebAPIBase - 위의 AD FS 섹션에서 앱 등록에서 #19의 식별자 값을 입력합니다.

    • ida:Authority - https://[AD FS 호스트 이름]/adfs 입력

      Screenshot of the web config file under ToDoListService showing the modified values.

  5. WebAPIOBO에서 Web.config 파일을 엽니다. 다음을 수정합니다.

    • ida: AdfsMetadataEndpoint - https://[AD FS 호스트 이름]/federationmetadata/2007-06/federationmetadata.xml 입력

    • ida:Audience - 위의 AD FS 섹션에서 앱 등록에서 #12의 클라이언트 식별자 값을 입력합니다.

      Screenshot of the web config file under WebAPIOBO showing the modified values.

샘플 테스트

이 섹션에서는 위에서 구성한 샘플을 테스트하는 방법을 보여줍니다.

코드가 변경되면 솔루션을 다시 빌드합니다.

  1. Visual Studio에서 솔루션을 마우스 오른쪽 단추로 클릭하고 시작 프로젝트 설정을 선택합니다 ...

    Screenshot of the list that appears when you right-click the solution with the Set Start Up Projects option highlighted.

  2. 속성 페이지에서 TodoListSPA를 제외한 각 프로젝트에 대해 작업이 시작으로 설정되어 있는지 확인합니다.

    Screenshot of the Solution Property Pages dialog box showing the Multiple startup project option selected and all of the projects' actions set to Start.

  3. Visual Studio 위쪽에서 녹색 화살표를 클릭합니다.

    Screenshot of the Visual Studio UI with the Start option called out.

  4. 네이티브 앱의 기본 화면에서 로그인클릭합니다.

    Screenshot of the To Do List Client dialog box.

    네이티브 앱 화면이 표시되지 않으면 시스템에 프로젝트 리포지토리가 저장된 폴더에서 *msalcache.bin 파일을 검색하고 제거합니다.

  5. AD FS 로그인 페이지로 리디렉션될 수 있습니다. 계속 해 서 로그인 합니다.

    Screenshot of the Sign In page.

  6. 로그인한 후 할 일 만들기 항목에서 Web Api 호출에 대한 텍스트 Web Api를 입력합니다. 항목 추가를 클릭합니다. 그러면 Web API(To Do List Service)를 호출한 다음 Web API 2(WebAPIOBO)를 호출하고 캐시에 항목을 추가합니다.

    Screenshot of the To Do List Client dialog box with the new to do item populating the To Do Items section.

다음 단계

AD FS OpenID Connect/OAuth 흐름 및 애플리케이션 시나리오