Compartir a través de


MicrosoftIdentityMessageHandler Constructor

Definition

Initializes a new instance of the MicrosoftIdentityMessageHandler class.

public MicrosoftIdentityMessageHandler(Microsoft.Identity.Abstractions.IAuthorizationHeaderProvider headerProvider, Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions? defaultOptions = default, Microsoft.Extensions.Logging.ILogger<Microsoft.Identity.Web.MicrosoftIdentityMessageHandler>? logger = default);
new Microsoft.Identity.Web.MicrosoftIdentityMessageHandler : Microsoft.Identity.Abstractions.IAuthorizationHeaderProvider * Microsoft.Identity.Web.MicrosoftIdentityMessageHandlerOptions * Microsoft.Extensions.Logging.ILogger<Microsoft.Identity.Web.MicrosoftIdentityMessageHandler> -> Microsoft.Identity.Web.MicrosoftIdentityMessageHandler
Public Sub New (headerProvider As IAuthorizationHeaderProvider, Optional defaultOptions As MicrosoftIdentityMessageHandlerOptions = Nothing, Optional logger As ILogger(Of MicrosoftIdentityMessageHandler) = Nothing)

Parameters

headerProvider
IAuthorizationHeaderProvider

The IAuthorizationHeaderProvider used to acquire authorization headers for outgoing requests. This is typically obtained from the dependency injection container.

defaultOptions
MicrosoftIdentityMessageHandlerOptions

Default authentication options that will be used for all requests unless overridden per-request using WithAuthenticationOptions(HttpRequestMessage, MicrosoftIdentityMessageHandlerOptions). If null, each request must specify its own authentication options or an exception will be thrown.

logger
ILogger<MicrosoftIdentityMessageHandler>

Optional logger for debugging and monitoring authentication operations. If provided, the handler will log information about token acquisition, challenges, and errors.

Exceptions

Thrown when headerProvider is null.

Examples

Basic usage with default options:

var handler = new MicrosoftIdentityMessageHandler(
    headerProvider,
    new MicrosoftIdentityMessageHandlerOptions 
    { 
        Scopes = { "https://api.example.com/.default" }
    });

Usage without default options (per-request configuration required):

var handler = new MicrosoftIdentityMessageHandler(headerProvider);

// Each request must specify options
var request = new HttpRequestMessage(HttpMethod.Get, "/api/data")
    .WithAuthenticationOptions(options => 
        options.Scopes.Add("custom.scope"));

Usage with logging:

var logger = serviceProvider.GetService<ILogger<MicrosoftIdentityMessageHandler>>();
var handler = new MicrosoftIdentityMessageHandler(headerProvider, defaultOptions, logger);

Remarks

The defaultOptions parameter provides a convenient way to set authentication options that apply to all requests made through this handler instance. Individual requests can still override these defaults using the extension methods.

When logger is provided, the handler will log at various levels:

  • Debug: Successful authorization header addition
  • Information: WWW-Authenticate challenge detection and handling
  • Warning: Challenge handling failures
  • Error: Token acquisition failures

Applies to