Language

MicrosoftIdentityHttpClientBuilderExtensions.AddMicrosoftIdentityMessageHandler メソッド

定義

オーバーロード

Name 説明
AddMicrosoftIdentityMessageHandler(IHttpClientBuilder)

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

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, MicrosoftIdentityMessageHandlerOptions)

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

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, Action<MicrosoftIdentityMessageHandlerOptions>)

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

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, IConfiguration, String)

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

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder)

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

public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder);
static member AddMicrosoftIdentityMessageHandler : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder
<Extension()>
Public Function AddMicrosoftIdentityMessageHandler (builder As IHttpClientBuilder) As IHttpClientBuilder

パラメーター

builder
IHttpClientBuilder

構成する IHttpClientBuilder 。

返品

メソッドチェーンの IHttpClientBuilder 。

例外

builderがnullされるとスローされます。

例

// Configure the HTTP client
services.AddHttpClient("ApiClient")
    .AddMicrosoftIdentityMessageHandler();

// Use the client with per-request configuration
public class MyService
{
    private readonly HttpClient _httpClient;

    public MyService(IHttpClientFactory factory)
    {
        _httpClient = factory.CreateClient("ApiClient");
    }

    public async Task<string> GetDataAsync()
    {
        var request = new HttpRequestMessage(HttpMethod.Get, "/api/data")
            .WithAuthenticationOptions(options =>
            {
                options.Scopes.Add("https://api.example.com/.default");
            });

        var response = await _httpClient.SendAsync(request);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}

注釈

このオーバーロードは、要求ごとに認証オプションを構成するための最大限の柔軟性が必要な場合に便利です。 既定のオプションは指定されていないため、すべての要求に、 WithAuthenticationOptions 拡張メソッドを使用した認証オプションを含める必要があります。

ハンドラーは、実行時にサービス プロバイダーからの IAuthorizationHeaderProvider を解決して、送信要求の承認ヘッダーを取得します。

適用対象

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, MicrosoftIdentityMessageHandlerOptions)

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

public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions options);
static member AddMicrosoftIdentityMessageHandler : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder * Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder
<Extension()>
Public Function AddMicrosoftIdentityMessageHandler (builder As IHttpClientBuilder, options As MicrosoftIdentityMessageHandlerOptions) As IHttpClientBuilder

パラメーター

builder
IHttpClientBuilder

構成する IHttpClientBuilder 。

options
MicrosoftIdentityMessageHandlerOptions

このクライアントによって行われたすべての要求に使用する認証オプション。

返品

メソッドチェーンの IHttpClientBuilder 。

例外

builderまたはoptionsがnullされたときにスローされます。

例

// Pre-configure options
var options = new MicrosoftIdentityMessageHandlerOptions
{
    Scopes = { "https://graph.microsoft.com/.default" }
};
options.WithAgentIdentity("agent-application-id");

// Configure the HTTP client with the pre-built options
services.AddHttpClient("GraphClient", client =>
{
    client.BaseAddress = new Uri("https://graph.microsoft.com");
})
.AddMicrosoftIdentityMessageHandler(options);

// Use the client - authentication is automatic
public class GraphService
{
    private readonly HttpClient _httpClient;

    public GraphService(IHttpClientFactory factory)
    {
        _httpClient = factory.CreateClient("GraphClient");
    }

    public async Task<string> GetUserProfileAsync()
    {
        var response = await _httpClient.GetAsync("/v1.0/me");
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}

注釈

このオーバーロードは、この HTTP クライアントによって行われたすべての要求に使用する必要がある事前構成済みの MicrosoftIdentityMessageHandlerOptions インスタンスがある場合に便利です。 個々の要求でも、要求ごとの拡張メソッドを使用して、これらの既定のオプションをオーバーライドできます。

ハンドラーは、実行時にサービス プロバイダーからの IAuthorizationHeaderProvider を解決して、送信要求の承認ヘッダーを取得します。

適用対象

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, Action<MicrosoftIdentityMessageHandlerOptions>)

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

public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, Action<Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions> configureOptions);
static member AddMicrosoftIdentityMessageHandler : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder * Action<Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions> -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder
<Extension()>
Public Function AddMicrosoftIdentityMessageHandler (builder As IHttpClientBuilder, configureOptions As Action(Of MicrosoftIdentityMessageHandlerOptions)) As IHttpClientBuilder

パラメーター

builder
IHttpClientBuilder

構成する IHttpClientBuilder 。

configureOptions
Action<MicrosoftIdentityMessageHandlerOptions>

認証オプションを構成するデリゲート。

返品

メソッドチェーンの IHttpClientBuilder 。

例外

builderまたはconfigureOptionsがnullされたときにスローされます。

例

// Configure the HTTP client with inline options configuration
services.AddHttpClient("MyApiClient", client =>
{
    client.BaseAddress = new Uri("https://api.example.com");
})
.AddMicrosoftIdentityMessageHandler(options =>
{
    options.Scopes.Add("https://api.example.com/.default");
    options.RequestAppToken = true;
});

// Use the client - authentication is automatic
public class ApiService
{
    private readonly HttpClient _httpClient;

    public ApiService(IHttpClientFactory factory)
    {
        _httpClient = factory.CreateClient("MyApiClient");
    }

    public async Task<string> GetDataAsync()
    {
        var response = await _httpClient.GetAsync("/api/data");
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}

エージェント ID の場合:

services.AddHttpClient("AgentClient")
    .AddMicrosoftIdentityMessageHandler(options =>
    {
        options.Scopes.Add("https://graph.microsoft.com/.default");
        options.WithAgentIdentity("agent-application-id");
        options.RequestAppToken = true;
    });

注釈

このオーバーロードは、認証オプションのインライン構成に役立ちます。 サービス構成中にデリゲートが 1 回呼び出され、HTTP クライアントの既定のオプションが作成されます。 個々の要求でも、要求ごとの拡張メソッドを使用して、これらの既定のオプションをオーバーライドできます。

ハンドラーは、実行時にサービス プロバイダーからの IAuthorizationHeaderProvider を解決して、送信要求の承認ヘッダーを取得します。

適用対象

AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, IConfiguration, String)

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

public static Microsoft.Extensions.DependencyInjection.IHttpClientBuilder AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder builder, Microsoft.Extensions.Configuration.IConfiguration configuration, string sectionName);
static member AddMicrosoftIdentityMessageHandler : Microsoft.Extensions.DependencyInjection.IHttpClientBuilder * Microsoft.Extensions.Configuration.IConfiguration * string -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder
<Extension()>
Public Function AddMicrosoftIdentityMessageHandler (builder As IHttpClientBuilder, configuration As IConfiguration, sectionName As String) As IHttpClientBuilder

パラメーター

builder
IHttpClientBuilder

構成する IHttpClientBuilder 。

configuration
IConfiguration

認証オプションを含む構成セクション。

sectionName
String

構成セクションの名前 (診断に使用)。

返品

メソッドチェーンの IHttpClientBuilder 。

例外

builder、configuration、またはsectionNameがnullされるとスローされます。

例

appsettings.jsonでの構成:

{
  "DownstreamApi": {
    "Scopes": ["https://api.example.com/.default"]
  },
  "GraphApi": {
    "Scopes": ["https://graph.microsoft.com/.default", "User.Read"]
  }
}

HTTP クライアントを構成します。

// In Program.cs or Startup.cs
services.AddHttpClient("DownstreamApiClient", client =>
{
    client.BaseAddress = new Uri("https://api.example.com");
})
.AddMicrosoftIdentityMessageHandler(
    configuration.GetSection("DownstreamApi"),
    "DownstreamApi");

services.AddHttpClient("GraphClient", client =>
{
    client.BaseAddress = new Uri("https://graph.microsoft.com");
})
.AddMicrosoftIdentityMessageHandler(
    configuration.GetSection("GraphApi"),
    "GraphApi");

クライアントを使用します。

public class MyService
{
    private readonly HttpClient _apiClient;
    private readonly HttpClient _graphClient;

    public MyService(IHttpClientFactory factory)
    {
        _apiClient = factory.CreateClient("DownstreamApiClient");
        _graphClient = factory.CreateClient("GraphClient");
    }

    public async Task<string> GetApiDataAsync()
    {
        var response = await _apiClient.GetAsync("/api/data");
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }

    public async Task<string> GetUserProfileAsync()
    {
        var response = await _graphClient.GetAsync("/v1.0/me");
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}

注釈

このオーバーロードは、appsettings.json またはその他の構成ソースから認証オプションを構成する場合に便利です。 構成セクションは、標準の構成バインドを使用して新しい MicrosoftIdentityMessageHandlerOptions インスタンスにバインドされます。 個々の要求でも、要求ごとの拡張メソッドを使用して、これらの既定のオプションをオーバーライドできます。

ハンドラーは、実行時にサービス プロバイダーからの IAuthorizationHeaderProvider を解決して、送信要求の承認ヘッダーを取得します。

適用対象