Learn how to build a web app signing-in users authenticated by AD FS 2019 and acquiring tokens using MSAL library to call web APIs.
이 문서를 읽기 전에 AD FS 개념 및 권한 부여 코드 부여 흐름에 대해 잘 알고 있어야 합니다.
Overview
이 흐름에서는 사용자를 로그인하고 웹 API를 호출할 수 있는 인증을 웹 앱(서버 앱)에 추가합니다. 웹 앱에서 Web API를 호출하려면 MSAL의 AcquireTokenByAuthorizationCode 토큰 획득 방법을 사용합니다. 권한 부여 코드 흐름을 사용하여 획득한 토큰을 토큰 캐시에 저장합니다. 그러면 컨트롤러가 필요할 때 캐시에서 자동으로 토큰을 획득합니다. MSAL이 필요할 때 토큰을 새로 고칩니다.
Web API를 호출하는 웹 앱
- 기밀 클라이언트 애플리케이션입니다.
- AD FS에 비밀(애플리케이션 공유 비밀, 인증서 또는 AD 계정)을 등록했습니다. 이 비밀은 토큰을 가져오기 위해 AD FS를 호출하는 동안 전달됩니다.
To register a Web App in AD FS and to configure it to acquire tokens to call a Web API, let's use a sample available here and walk through the app registration and code configuration steps.
Pre-requisites
- GitHub 클라이언트 도구
- AD FS 2019 이상 구성 및 실행
- Visual Studio 2013 이상
AD FS에 앱 등록
이 섹션은 웹 앱을 기밀 클라이언트로 등록하고 Web API를 AD FS에서 신뢰 당사자(RP)로 등록하는 방법을 설명합니다.
In AD FS Management, right-click on Application Groups and select Add Application Group.
On the Application Group Wizard, for the Name enter WebAppToWebApi and under Client-Server applications select the Server application accessing a Web API template. Click Next.
Copy the Client Identifier value. The value is used later as the value for ida:ClientId in the applications Web.config file. Enter the following for Redirect URI: - https://localhost:44326. Click Add. Click Next.
애플리케이션 자격 증명 구성 화면에서 공유 비밀 생성에 체크 표시를 하고 비밀을 복사합니다. This secret is used later as the value for ida:ClientSecret in the applications Web.config file. Click Next.
On the Configure Web API screen, enter the Identifier:https://webapi. Click Add. Click Next. This value is used later for ida:GraphResourceId in the applications Web.config file.
On the Apply Access Control Policy screen, select Permit everyone and click Next.
On the Configure Application Permissions screen, make sure openid and user_impersonation are selected and click Next.
On the Summary screen, click Next.
On the Complete screen, click Close.
Code Configuration
이 섹션은 사용자를 로그인하도록 ASP.NET 웹 앱을 구성하고 Web API를 호출하기 위한 토큰을 검색하는 방법을 설명합니다.
Download the sample from here
Visual Studio를 사용하여 샘플 열기
web.config 파일을 엽니다. 다음을 수정합니다.
ida:ClientId
- enter the Client Identifier value from #3 in App Registration in AD FS section.ida:ClientSecret
- enter the Secret value from #4 in App Registration in AD FS section.ida:RedirectUri
- enter the Redirect URI value from #3 in App Registration in AD FS section.ida:Authority
- https://[your AD FS hostname]/adfs를 입력합니다. 예:https://adfs.contoso.com/adfs
ida:Resource
- enter the Identifier value from #5 in App Registration in AD FS section.
샘플 테스트
이 섹션은 구성된 샘플을 테스트하는 방법을 보여줍니다.
코드가 변경되면 솔루션을 다시 빌드합니다.
Visual Studio 맨 위에 있는 Internet Explorer가 선택 되어 있는지 확인 하 고 녹색 화살표를 클릭 합니다.
홈페이지에서 로그인을 클릭합니다.
AD FS 로그인 페이지로 리디렉션됩니다. 로그인하세요.
로그인 후 액세스 토큰을 클릭합니다.
액세스 토큰을 클릭하면 Web API를 호출하여 액세스 토큰 정보를 가져옵니다.