次の方法で共有


Web API を呼び出す Web API (On-Behalf-Of シナリオ)

適用対象: Windows Server 2022、Windows Server 2019、AD FS 2019 以降

ユーザーの代わりに別の Web API を呼び出す Web API を構築する方法について説明します。

この記事を読む前に、ADFS の概念認証コードの付与フローについて理解しておく必要があります

概要

  • 次の図に示されていないクライアント (Web アプリ) は、保護された Web API を呼び出し、その "Authorization" Http ヘッダーに JWT ベアラー トークンを提供します。

  • 保護された Web API はトークンを検証し、MSAL AcquireTokenOnBehalfOf メソッドを使用して (AD FS から) 別のトークンを要求し、それ自体がユーザーに代わって 2 つ目の Web API (ダウンストリーム Web API という名前) を呼び出すのを可能にしています。

  • 保護された Web 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. アプリケーション グループ ウィザードで、[名前] に「NativeAppToWebApi」と入力し、[クライアント/サーバー アプリケーション][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. [Web API の構成] 画面で、[識別子] に次のように入力します: https://localhost:44321/. [追加] をクリックします。 [次へ] をクリックします。 この値は、後でアプリのファイル App.config および Web.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 の管理] で、[アプリケーション グループ] をクリックし、[NativeAppToWebApi] アプリグループを選択します。 右クリックし、 [プロパティ]を選択します。

    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. [サーバー アプリケーション] 画面で、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 の管理] で、[アプリケーション グループ] をクリックし、[NativeAppToWebApi] アプリグループを選択します。 右クリックし、 [プロパティ]を選択します。

    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 のプロパティ] 画面で [OK] をクリックします

  25. [WebApiToWebApi のプロパティ] 画面で、[ WebApiToWebApi – Web API ] を選択し、[編集 ] をクリックします

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

  26. [NativeAppToWebApi – 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」と入力し、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 2 のプロパティ] 画面で [OK] をクリックします

  30. [WebApiToWebApi プロパティ] 画面で、[WebApiToWebApi – Web API 2] を選択し、[編集...]をクリックします
    Screenshot of the WebApiToWebApi Properties dialog box showing the WebApiToWebApi - Web A P I 2 application highlighted.

  31. [NativeAppToWebApi – 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」と入力し、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 のプロパティ] 画面で [OK] をクリックし、[WebApiToWebApi のプロパティ] 画面をクリックします。

コード構成

このセクションでは、別の Web API を呼び出すように Web API を構成する方法について説明します

  1. こちらからサンプルをダウンロードする

  2. Visual Studio を使用してサンプルを開く

  3. App.config ファイルを開きます。 次の変更を行います:

    • ida:Authority - https://[AD FS hostname]/adfs/ を入力します

    • ida:ClientId - 上記の AD FS セクションの [アプリの登録] の #3 からの[クライアント識別子] 値を入力します。

    • ida:RedirectUri - 上記の AD FS セクションの [アプリの登録] の #3 からの [リダイレクト URI]値を入力します。

    • 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 - #13 からコピーした共有シークレットを、上記の「アプリの登録」セクションAD FS入力します。

    • ida:RedirectUri - 上記の AD FS セクションの [アプリの登録] の #12 からの [リダイレクト URI] 値を入力します。

    • ida: AdfsMetadataEndpoint - https://[your AD FS hostname]/federationmetadata/2007-06/federationmetadata.xml

    • ida:Audience – 上記の AD FS セクションの [アプリの登録] の #19 からの [識別子] 値を入力します。

    • ida:Authority - https://[AD FS hostname]/adfs/ を入力します

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

  5. WebAPIOBO Web.configファイルを開きます。 次の変更を行います:

    • ida: AdfsMetadataEndpoint - https://[your AD FS hostname]/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. [プロパティ] ページで、各プロジェクトに対して [アクション][開始] に設定されていることを確認します。

    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. サインインしたら、[Create a To Do] 項目に「Web Api to Web Api Call in the」というテキストを入力します[項目の追加] をクリックします。 これにより、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 接続/OAuth フローとアプリケーション シナリオ