Share via


MicrosoftIdentityAuthenticationException Class

Definition

Exception thrown when authentication fails during HTTP message handling by MicrosoftIdentityMessageHandler.

public class MicrosoftIdentityAuthenticationException : Exception
type MicrosoftIdentityAuthenticationException = class
    inherit Exception
Public Class MicrosoftIdentityAuthenticationException
Inherits Exception
Inheritance
MicrosoftIdentityAuthenticationException

Examples

Typical exception handling pattern:

try
{
    var response = await httpClient.SendAsync(request, cancellationToken);
    response.EnsureSuccessStatusCode();
}
catch (MicrosoftIdentityAuthenticationException authEx)
{
    // Handle authentication-specific failures
    logger.LogError(authEx, "Authentication failed: {Message}", authEx.Message);
    throw; // Re-throw or handle as appropriate
}
catch (HttpRequestException httpEx)
{
    // Handle other HTTP-related failures
    logger.LogError(httpEx, "HTTP request failed: {Message}", httpEx.Message);
}

Remarks

This exception is typically thrown in the following scenarios:

  • No authentication options are configured (neither default nor per-request)
  • No scopes are specified in the authentication options
  • Token acquisition fails due to authentication provider issues

Note on WWW-Authenticate Challenge Handling: When a downstream API returns a 401 Unauthorized response with a WWW-Authenticate header containing additional claims (e.g., for Conditional Access), the handler automatically extracts these claims using WwwAuthenticateParameters and attempts to acquire a new token with the requested claims. If this automatic retry succeeds, no exception is thrown. If the retry also fails with a 401, the response is returned to the caller without throwing an exception - the caller should check the status code. Exceptions are only thrown for token acquisition failures, not for HTTP 401 responses themselves.

When handling this exception, examine the Message property for specific details about what caused the authentication failure. If an inner exception is present, it may contain additional information from the underlying authentication provider.

Constructors

Name Description
MicrosoftIdentityAuthenticationException(String, Exception)

Initializes a new instance of the MicrosoftIdentityAuthenticationException class with a specified error message and a reference to the inner exception that is the cause of this exception.

MicrosoftIdentityAuthenticationException(String)

Initializes a new instance of the MicrosoftIdentityAuthenticationException class with a specified error message.

Applies to

See also