Repository navigation
Description
Microsoft.Identity.Web Library
Microsoft.Identity.Web.TokenAcquisition
Microsoft.Identity.Web version
4.18.0
Web app
Not Applicable
Web API
Protected web APIs call downstream web APIs
Token cache serialization
In-memory caches
Description
A web API configured with an Authority (and no Instance/TenantId) can't call a downstream API On-Behalf-Of the user when the user's token has a tid claim, which every Entra ID v2.0 token has. Token acquisition throws MsalClientException: authority_type_mismatch. This also happens with Entra ID itself, https://login.microsoftonline.com//v2.0 (reproduced, see below). The same code is on current master (432c6da).
Cause
- AddMicrosoftIdentityWebApi sets PreserveAuthority = true for an Authority that isn't a CIAM root (AuthorityHelpers.BuildCiamAuthorityIfNeeded). TokenAcquisition.BuildConfidentialClientApplicationAsync then builds the app with WithOidcAuthority(authority), which MSAL treats as a Generic authority.
- In GetAuthenticationResultForWebApiToCallDownstreamApiAsync, TokenAcquisition reads tid from _tokenAcquisitionHost.GetUserFromRequest() and calls builder.WithTenantId(userTenant) whenever it is present.
- In MSAL, GenericAuthority.GetTenantedAuthority returns the authority unchanged ("Assume generic authorities are not tenanted"). AuthorityInfo.AuthorityInfoHelper.CreateAuthorityWithTenant then re-parses that URL with Authority.CreateAuthority. GetAuthorityType classifies any URL whose first path segment isn't adfs, dstsv2 or tfp as Aad, so the request authority is Aad while the app's is Generic, and ValidateTypeMismatch throws.
The code path doesn't depend on the host, so it affects any OIDC-compatible authority configured with Authority. For Entra ID, Instance + TenantId should avoid it, because that path builds the app with WithAuthority (not part of the repro below). It doesn't help for an OIDC-compatible authority on another host: there WithAuthority makes MSAL run instance discovery against login.microsoftonline.com, which answers invalid_instance, and Microsoft.Identity.Web doesn't expose WithInstanceDiscovery(false) or validateAuthority: false in configuration.
Proposed fix
For a preserved authority, don't apply the user's home tenant on the OBO request: a Generic authority has no tenant to override. An explicitly requested tenant (AcquireTokenOptions.Tenant) keeps today's behaviour. A pull request with this change and a unit test follows.
How this was found
Found while building an example against HeyIdentity, an Entra-compatible test identity provider. On a host other than Microsoft's, Authority is the only Microsoft.Identity.Web configuration that can acquire tokens.
Reproduction steps
- Configure a web API with an Authority and no Instance/TenantId, and enable token acquisition (see the code snippets).
- Call ITokenAcquisition.GetAccessTokenForUserAsync(scopes) or IDownstreamApi.CallApiForUserAsync inside a request whose user token has a tid claim.
- Token acquisition throws authority_type_mismatch.
Reproduced with Microsoft.Identity.Web 4.18.0 against Entra ID: a web API registration (v2.0 access tokens, exposed scope) calling a downstream API registration with admin-consented delegated scopes, and a v2.0 user token (iss=https://login.microsoftonline.com/<tenant id>/v2.0, tid present). Every OBO call fails.
Error message
MsalClientException: authority_type_mismatch
A authority of type Generic was used at the application and of type Aad at the request level.
Please use the same authority type between the two.
Id Web logs
No response
Relevant code snippets
`
"AzureAd": {
"Authority": "https://login.microsoftonline.com/<tenant id>/v2.0",
"ClientId": "<web API client id>",
"ClientCredentials": [ { "SourceType": "ClientSecret", "ClientSecret": "<secret>" } ]
}`
`
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(configuration.GetSection("AzureAd"))
.EnableTokenAcquisitionToCallDownstreamApi()
.AddInMemoryTokenCaches();
// In a request handler of the web API:
string token = await tokenAcquisition.GetAccessTokenForUserAsync(["api://<downstream api>/.default"]);
`Regression
Not known
Expected behavior
An OBO token from the token endpoint in <authority>'s discovery document.
Activity
bgavrilMS commentedon Nov 26, 2026
Yeah, you should use "Instance" and "Tenant" for AAD. It was a mistake on our part to try to model a general purpose authority and to distinguish internally between AAD and more generic OIDC authorities.
bgavrilMS commentedon Nov 26, 2026
The correct behavior is to use Instance and Tenant in the config.
Maybe it's better to get errors at this stage and fix the config. Going with "oidc authority" screws up the tenancy and can introduce errors which are much trickier.
Iarek Kovtunenko (@iarekk) - this supports the refactoring that you were trying to do. The WebAPI scenario is broken - this is evidence. So we don't hvae to worry about our internal web api library.
I think it's acceptable to require ppl to use instance and tenant for AAD authority all the time. AAD authority has special rules.