Language

MicrosoftIdentityHttpClientBuilderExtensions クラス

定義

さまざまな構成オプションを使用して HTTP クライアント パイプラインにMicrosoftIdentityMessageHandlerを追加するIHttpClientBuilderの拡張メソッド。

public static class MicrosoftIdentityHttpClientBuilderExtensions
type MicrosoftIdentityHttpClientBuilderExtensions = class
Public Module MicrosoftIdentityHttpClientBuilderExtensions
継承
MicrosoftIdentityHttpClientBuilderExtensions

例

インライン構成での基本的な使用方法:

services.AddHttpClient("MyApiClient", client =>
{
    client.BaseAddress = new Uri("https://api.example.com");
})
.AddMicrosoftIdentityMessageHandler(options =>
{
    options.Scopes.Add("https://api.example.com/.default");
});

appsettings.jsonからの構成:

// In appsettings.json:
// {
//   "DownstreamApi": {
//     "Scopes": ["https://graph.microsoft.com/.default"]
//   }
// }

services.AddHttpClient("GraphClient")
    .AddMicrosoftIdentityMessageHandler(
        configuration.GetSection("DownstreamApi"),
        "DownstreamApi");

要求ごとの構成のパラメーターなし:

services.AddHttpClient("FlexibleClient")
    .AddMicrosoftIdentityMessageHandler();

// Later, in a service:
var request = new HttpRequestMessage(HttpMethod.Get, "/api/data")
    .WithAuthenticationOptions(options =>
    {
        options.Scopes.Add("custom.scope");
    });
var response = await httpClient.SendAsync(request);

注釈

これらの拡張メソッドは、MicrosoftIdentityMessageHandlerを使用した自動Microsoft ID 認証を使用して HttpClient インスタンスを構成する便利な方法を提供します。 ハンドラーは、構成されたオプションに基づいて、送信 HTTP 要求に承認ヘッダーを自動的に追加します。

さまざまな構成シナリオをサポートするために、次の 4 つのオーバーロードが用意されています。

  • パラメーターなし: 要求ごとにオプションが設定されるシナリオの場合
  • Options インスタンス: 事前構築済みの options オブジェクトを使用したプログラムによる構成の場合
  • アクション デリゲート: 構成デリゲートを使用したインライン構成の場合
  • IConfiguration: appsettings.json またはその他の構成ソースからの構成の場合

メソッド

Name 説明
AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, Action<MicrosoftIdentityMessageHandlerOptions>)

デリゲートを使用して構成されたオプションを使用して、HTTP クライアント パイプラインに MicrosoftIdentityMessageHandler を追加します。

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, IConfiguration, String)

IConfiguration からバインドされたオプションを使用して、HTTP クライアント パイプラインに MicrosoftIdentityMessageHandler を追加します。

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, MicrosoftIdentityMessageHandlerOptions)

指定したオプションを使用して、HTTP クライアント パイプラインに MicrosoftIdentityMessageHandler を追加します。

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder)

既定のオプションなしで HTTP クライアント パイプラインに MicrosoftIdentityMessageHandler を追加します。 オプションは、 WithAuthenticationOptions(HttpRequestMessage, MicrosoftIdentityMessageHandlerOptions)を使用して要求ごとに構成する必要があります。

適用対象

関連項目