共用方式為


情節:Web 應用程式 (伺服器應用程式) 呼叫 Web API

適用於:Windows Server 2022、Windows Server 2019、AD FS 2019 及更新版本

了解如何建置由 AD FS 2019 驗證登入使用者,並使用 MSAL 程式庫取得權杖來呼叫 Web API 的 Web 應用程式。

閱讀本文之前,您應該先熟悉 AD FS 概念授權碼授與流程

概觀

Overview of web app calling web api

您在這段流程中驗證功能加入 Web 應用程式 (伺服器應用程式),因此可登入使用者和呼叫 Web API。 若要從 Web 應用程式呼叫 Web API,請使用 MSAL 的 AcquireTokenByAuthorizationCode 權杖取得方法。 您使用授權代碼流,將取得的權杖儲存在權杖快取中。 接著控制器必要時會悄悄從快取中取得權杖。 MSAL 會視需要重新整理權杖。

呼叫 Web API 的 Web 應用程式:

  • 為機密用戶端應用程式。
  • 與 AD FS 註冊秘密 (應用程式共享的秘密、證書或 AD 帳戶)。 此秘密會在呼叫 AD FS 期間傳入,以取得權杖。

若要在 AD FS 註冊 Web 應用程式並配置為取得權杖呼叫 Web API,請使用這裡提供的例子,並實地演練應用程式註冊和代碼配置步驟。

必要條件

  • GitHub 用戶端工具
  • 已設定且正在執行的 AD FS 2019 或更新版本
  • Visual Studio 2013 或更新版本

AD FS 中的應用程式註冊

本節說明如何在 AD FS 中將 Web 應用程式註冊為機密用戶端,以及如何將 Web API 註冊為信賴憑證者 (RP)。

  1. 在 AD FS 管理中以滑鼠右鍵按一下 [應用程式群組],然後選取 [新增應用程式群組]

  2. 在 [應用程式群組精靈] 上,針對 [名稱] 輸入 WebAppToWebApi,然後在 [用戶端伺服器應用程式] 下選取 [存取 Web API 的伺服器應用程式] 範本。 按一下 [下一步] 。

    Screenshot of the Welcome page of the Add Application Group Wizard showing the Server application accessing a web A P I template highlighted.

  3. 複製 [用戶端識別碼] 值。 此數值稍候會當成應用程式 Web.config 檔案裡 ida:ClientId 的數值。 針對 [重新導向 URI:],輸入下列內容 - https://localhost:44326. 按一下新增。 按一下 [下一步] 。

    Screenshot of the Server application page of the Add Application Group Wizard showing the correct client identifier and redirect U R I.

  4. 在配置應用程式憑證螢幕畫面上放置簽到 產生共享秘密並複製此秘密。 此數值稍候會當成應用程式 Web.config 檔案裡 ida:ClientId 的數值。 按一下 [下一步] 。

    Screenshot of the Configure Application Credentials application page of the Add Application Group Wizard showing the Generate a shared secret option selected and the generated shared secret populated.

  5. 在 [設定 Web API] 畫面上,輸入 [識別碼:]https://webapi. 按一下新增。 按一下 [下一步] 。 此數值稍候會用於應用程式 Web.config 檔案裡的 ida:GraphResourceId

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

  6. 在 [套用存取控制原則] 畫面上,選取 [允許所有人],然後按 [下一步]

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

  7. 在 [設定應用程式權限] 畫面上,確定已選取 [openid] 和 [user_impersonation],然後按 [下一步]

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

  8. 在 [摘要] 畫面上,按 [下一步]

  9. 在 [完成] 畫面上,按一下 [關閉]

程式碼設定

本節說明如何設定 ASP.NET Web 應用程式來登入使用者,以及如何擷取權杖以呼叫 Web API

  1. 這裡下載範例

  2. 使用 Visual Studio 開啟範例

  3. 開啟 Web.config 檔案。 修改下列內容:

    • ida:ClientId - 在 AD FS 應用註冊區 #3 輸入客戶端識別碼數值。

    • ida:ClientSecret - 在 AD FS 應用註冊區 #4 輸入秘密數值。

    • ida:RedirectUri - 在 AD FS 應用註冊區 #3 輸入轉向 URL 數值。

    • ida:Authority - 輸入 https://[您的 AD FS 主機名稱]/adfs。 例如,https://adfs.contoso.com/adfs

    • ida:Resource - 在 AD FS 應用註冊區 #5 輸入識別碼數值。

      Screenshot of the web config file showing the modified values.

測試範例

本節展示如何測試配置的樣本。

  1. 變更代碼後重新建置解決方案。

  2. 在 Visual Studio 頂端,確定已選取 Internet Explorer,然後按一下綠色箭號。

    Screenshot of the Visual Studio UI with the IIS Express (Internet Explorer) option called out.

  3. 在首頁上,按一下 [登入]。

    Screenshot of the Home Page with the Sign in option called out.

  4. 您將被重新轉向到 AD FS 登入頁。 繼續登入。

    Screenshot of the Sign in page.

  5. 一旦登入之後,按一下 [存取權杖]。

    Screenshot of the Home Page with the Access Token option called out.

  6. 點選存取權杖即可藉由呼叫 Web API 取得存取權杖資訊。

    Screenshot of the Access Token page showing the access token info.

後續步驟

AD FS OpenID Connect/OAuth 流程和應用程式案例