MicrosoftIdentityHttpClientBuilderExtensions Class
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Extension methods for IHttpClientBuilder to add MicrosoftIdentityMessageHandler to the HTTP client pipeline with various configuration options.
public static class MicrosoftIdentityHttpClientBuilderExtensions
type MicrosoftIdentityHttpClientBuilderExtensions = class
Public Module MicrosoftIdentityHttpClientBuilderExtensions
- Inheritance
-
MicrosoftIdentityHttpClientBuilderExtensions
Examples
Basic usage with inline configuration:
services.AddHttpClient("MyApiClient", client =>
{
client.BaseAddress = new Uri("https://api.example.com");
})
.AddMicrosoftIdentityMessageHandler(options =>
{
options.Scopes.Add("https://api.example.com/.default");
});
Configuration from appsettings.json:
// In appsettings.json:
// {
// "DownstreamApi": {
// "Scopes": ["https://graph.microsoft.com/.default"]
// }
// }
services.AddHttpClient("GraphClient")
.AddMicrosoftIdentityMessageHandler(
configuration.GetSection("DownstreamApi"),
"DownstreamApi");
Parameterless for per-request configuration:
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);
Remarks
These extension methods provide a convenient way to configure HttpClient instances with automatic Microsoft Identity authentication using MicrosoftIdentityMessageHandler. The handler will automatically add authorization headers to outgoing HTTP requests based on the configured options.
Four overloads are provided to support different configuration scenarios:
- Parameterless: For scenarios where options are set per-request
- Options instance: For programmatic configuration with a pre-built options object
- Action delegate: For inline configuration using a configuration delegate
- IConfiguration: For configuration from appsettings.json or other configuration sources
Methods
| Name | Description |
|---|---|
| AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, Action<MicrosoftIdentityMessageHandlerOptions>) |
Adds a MicrosoftIdentityMessageHandler to the HTTP client pipeline with options configured via delegate. |
| AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, IConfiguration, String) |
Adds a MicrosoftIdentityMessageHandler to the HTTP client pipeline with options bound from IConfiguration. |
| AddMicrosoftIdentityMessageHandler(IHttpClientBuilder, MicrosoftIdentityMessageHandlerOptions) |
Adds a MicrosoftIdentityMessageHandler to the HTTP client pipeline with the specified options. |
| AddMicrosoftIdentityMessageHandler(IHttpClientBuilder) |
Adds a MicrosoftIdentityMessageHandler to the HTTP client pipeline with no default options. Options must be configured per-request using WithAuthenticationOptions(HttpRequestMessage, MicrosoftIdentityMessageHandlerOptions). |