Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 6 additions & 12 deletions docs/calling-downstream-apis/custom-apis.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,16 +367,14 @@ services.AddHttpClient("DownstreamApiClient", client =>
client.BaseAddress = new Uri("https://api.example.com");
})
.AddMicrosoftIdentityMessageHandler(
configuration.GetSection("DownstreamApi"),
"DownstreamApi");
configuration.GetSection("DownstreamApi"));

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

### Configuration Examples
Expand Down Expand Up @@ -479,24 +477,21 @@ services.AddHttpClient("CustomerApiClient", client =>
client.BaseAddress = new Uri("https://customer-api.example.com");
})
.AddMicrosoftIdentityMessageHandler(
downstreamApis.GetSection("CustomerApi"),
"CustomerApi");
downstreamApis.GetSection("CustomerApi"));

services.AddHttpClient("OrderApiClient", client =>
{
client.BaseAddress = new Uri("https://order-api.example.com");
})
.AddMicrosoftIdentityMessageHandler(
downstreamApis.GetSection("OrderApi"),
"OrderApi");
downstreamApis.GetSection("OrderApi"));

services.AddHttpClient("InventoryApiClient", client =>
{
client.BaseAddress = new Uri("https://inventory-api.example.com");
})
.AddMicrosoftIdentityMessageHandler(
downstreamApis.GetSection("InventoryApi"),
"InventoryApi");
downstreamApis.GetSection("InventoryApi"));
```

### Per-Request Options
Expand Down Expand Up @@ -614,8 +609,7 @@ services.AddHttpClient("SecureApiClient", client =>
client.BaseAddress = new Uri("https://secure-api.example.com");
})
.AddMicrosoftIdentityMessageHandler(
configuration.GetSection("DownstreamApis:SecureApi"),
"SecureApi");
configuration.GetSection("DownstreamApis:SecureApi"));
```

##### Inline Configuration
Expand Down
3 changes: 1 addition & 2 deletions docs/calling-downstream-apis/token-binding.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,8 +409,7 @@ services.AddHttpClient("SecureApiClient", client =>
client.BaseAddress = new Uri("https://secure-api.example.com");
})
.AddMicrosoftIdentityMessageHandler(
configuration.GetSection("DownstreamApis:SecureApi"),
"SecureApi");
configuration.GetSection("DownstreamApis:SecureApi"));
```

#### Per-Request Token Binding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Microsoft.Identity.Web
/// will automatically add authorization headers to outgoing HTTP requests based on the configured options.
/// </para>
/// <para>
/// Four overloads are provided to support different configuration scenarios:
/// The overloads support different configuration scenarios:
/// </para>
/// <list type="bullet">
/// <item><description>Parameterless: For scenarios where options are set per-request</description></item>
Expand Down Expand Up @@ -58,8 +58,7 @@ namespace Microsoft.Identity.Web
///
/// services.AddHttpClient("GraphClient")
/// .AddMicrosoftIdentityMessageHandler(
/// configuration.GetSection("DownstreamApi"),
/// "DownstreamApi");
/// configuration.GetSection("DownstreamApi"));
/// </code>
///
/// <para><strong>Parameterless for per-request configuration:</strong></para>
Expand Down Expand Up @@ -325,10 +324,9 @@ public static IHttpClientBuilder AddMicrosoftIdentityMessageHandler(
/// </summary>
/// <param name="builder">The <see cref="IHttpClientBuilder"/> to configure.</param>
/// <param name="configuration">The configuration section containing the authentication options.</param>
/// <param name="sectionName">The name of the configuration section (used for diagnostics).</param>
/// <returns>The <see cref="IHttpClientBuilder"/> for method chaining.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="builder"/>, <paramref name="configuration"/>, or <paramref name="sectionName"/> is <see langword="null"/>.
/// Thrown when <paramref name="builder"/> or <paramref name="configuration"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// <para>
Expand Down Expand Up @@ -363,16 +361,14 @@ public static IHttpClientBuilder AddMicrosoftIdentityMessageHandler(
/// client.BaseAddress = new Uri("https://api.example.com");
/// })
/// .AddMicrosoftIdentityMessageHandler(
/// configuration.GetSection("DownstreamApi"),
/// "DownstreamApi");
/// configuration.GetSection("DownstreamApi"));
///
/// services.AddHttpClient("GraphClient", client =>
/// {
/// client.BaseAddress = new Uri("https://graph.microsoft.com");
/// })
/// .AddMicrosoftIdentityMessageHandler(
/// configuration.GetSection("GraphApi"),
/// "GraphApi");
/// configuration.GetSection("GraphApi"));
/// </code>
///
/// <para><strong>Use the clients:</strong></para>
Expand Down Expand Up @@ -406,6 +402,39 @@ public static IHttpClientBuilder AddMicrosoftIdentityMessageHandler(
/// </example>
[RequiresUnreferencedCode("Calls Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object).")]
[RequiresDynamicCode("Calls Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object).")]
public static IHttpClientBuilder AddMicrosoftIdentityMessageHandler(
this IHttpClientBuilder builder,
IConfiguration configuration)
{
if (builder is null)
{
throw new ArgumentNullException(nameof(builder));
}

if (configuration is null)
{
throw new ArgumentNullException(nameof(configuration));
}

return builder.AddMicrosoftIdentityMessageHandler(options => configuration.Bind(options));
Comment thread
jmprieur marked this conversation as resolved.
Resolved
}

/// <summary>
/// Adds a <see cref="MicrosoftIdentityMessageHandler"/> to the HTTP client pipeline with options bound from IConfiguration.
/// </summary>
/// <param name="builder">The <see cref="IHttpClientBuilder"/> to configure.</param>
/// <param name="configuration">The configuration section containing the authentication options.</param>
/// <param name="sectionName">Retained for compatibility. This value is checked for null but is otherwise unused.</param>
/// <returns>The <see cref="IHttpClientBuilder"/> for method chaining.</returns>
/// <exception cref="ArgumentNullException">
/// Thrown when <paramref name="builder"/>, <paramref name="configuration"/>, or <paramref name="sectionName"/> is <see langword="null"/>.
/// </exception>
/// <remarks>
/// The supplied <paramref name="configuration"/> is bound directly; <paramref name="sectionName"/> does not select a section or name options.
/// Prefer <see cref="AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, IConfiguration)"/> for new code.
/// </remarks>
[RequiresUnreferencedCode("Calls Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object).")]
[RequiresDynamicCode("Calls Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration, Object).")]
public static IHttpClientBuilder AddMicrosoftIdentityMessageHandler(
this IHttpClientBuilder builder,
IConfiguration configuration,
Expand All @@ -426,18 +455,7 @@ public static IHttpClientBuilder AddMicrosoftIdentityMessageHandler(
throw new ArgumentNullException(nameof(sectionName));
}

return builder.AddHttpMessageHandler(sp =>
{
var options = new MicrosoftIdentityMessageHandlerOptions();
configuration.Bind(options);

return new MicrosoftIdentityMessageHandler(
sp.GetRequiredService<IAuthorizationHeaderProvider>(),
options,
GetMsalMtlsHttpClientFactory(sp),
GetCredentialsProvider(sp),
GetLogger<MicrosoftIdentityMessageHandler>(sp));
});
return builder.AddMicrosoftIdentityMessageHandler(configuration);
}

private static IMsalMtlsHttpClientFactory? GetMsalMtlsHttpClientFactory(IServiceProvider services)
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Microsoft.Identity.Web.MicrosoftIdentityHttpClientBuilderExtensions.AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder! builder, Microsoft.Extensions.Configuration.IConfiguration! configuration) -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder!
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
static Microsoft.Identity.Web.MicrosoftIdentityHttpClientBuilderExtensions.AddMicrosoftIdentityMessageHandler(this Microsoft.Extensions.DependencyInjection.IHttpClientBuilder! builder, Microsoft.Extensions.Configuration.IConfiguration! configuration) -> Microsoft.Extensions.DependencyInjection.IHttpClientBuilder!
3 changes: 1 addition & 2 deletions tests/DevApps/MtlsPop/MtlsPopClient/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public static async Task Main(string[] args)
tokenAcquirerFactory.Configuration.GetSection("WebApi")["BaseUrl"]!);
})
.AddMicrosoftIdentityMessageHandler(
tokenAcquirerFactory.Configuration.GetSection("WebApi"),
"WebApi");
tokenAcquirerFactory.Configuration.GetSection("WebApi"));

tokenAcquirerFactory.Services.AddMicrosoftGraph();

Expand Down
Loading