Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Security and hardening

ASP.NET Core sign-in looks fine, but calling the downstream API throws MsalUiRequiredException

Your error

MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call.1 more line it can show

Fix it fast

Most common cause, 1 of 4

Nothing turns the exception into a new sign-in challenge

Put the attribute on every controller or page model that acquires a token, with the scopes it needs.

Fix
[Authorize]
[AuthorizeForScopes(Scopes = new[] { "User.Read" })]
public class HomeController : Controller
{
    // actions that call ITokenAcquisition, GraphServiceClient or IDownstreamApi
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Microsoft.Identity.Web throws MsalUiRequiredException (No account or login hint was passed to the AcquireTokenSilent call) when the auth cookie shows a signed-in user but the token cache holds nothing for them. A restart that empties an in-memory cache is the usual trigger. Let AuthorizeForScopes or the Blazor consent handler re-challenge the user; keep user tokens in a distributed cache.

  • Applies to Microsoft.Identity.Web 4.18, Microsoft.Identity.Client (MSAL.NET) 4.92, ASP.NET Core 10
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

List how the app stores tokens and where it handles the challenge. Which of these names appear, and which are missing, decides between the causes.

Run this first
grep -rnE "AddInMemoryTokenCaches|AddDistributedTokenCaches|AuthorizeForScopes|AddMicrosoftIdentityConsentHandler|ConsentHandler.HandleException" --include=*.cs --include=*.razor --include=*.cshtml .

Then find the line you see in this list and open its fix:

  1. 1Nothing turns the exception into a new sign-in challengeThe error page shows IDW10502 for users who stayed signed in across a deploy, and grep finds no AuthorizeForScopes on the controller or Razor page
  2. 2User tokens live in an in-memory cache that restarts and scale-out wipeThe error follows every app recycle, or appears only on some requests behind a load balancer with several instances
  3. 3A Blazor Server component calls the API without the consent handlerThe exception appears inside a Blazor component, where [AuthorizeForScopes] has no effect
  4. 4The code asks for a scope the tenant administrator never approvedThe exception comes back for one feature even after a fresh sign-in, while other API calls keep working

What does the error look like?

  • MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call.
  • Microsoft.Identity.Web.MicrosoftIdentityWebChallengeUserException: IDW10502: An MsalUiRequiredException was thrown due to a challenge for the user. See https://aka.ms/ms-id-web/ca_incremental-consent.

Cause 1 of 4

Nothing turns the exception into a new sign-in challenge

You seeThe error page shows IDW10502 for users who stayed signed in across a deploy, and grep finds no AuthorizeForScopes on the controller or Razor page

Check

MsalUiRequiredException reaches the user because no filter catches it. The Microsoft.Identity.Web wiki calls the exception expected when the cookie outlives the cached token, and says MVC controllers and Razor pages should carry [AuthorizeForScopes]. That attribute is an exception filter: it catches the MSAL exception and sends the browser to sign in again, usually without a prompt. A try/catch that swallows or wraps the exception hides it from the filter.

Check
grep -rn "catch (MsalUiRequiredException" --include=*.cs .

Fix

Put the attribute on every controller or page model that acquires a token, with the scopes it needs. Inside a catch block, rethrow MSAL exceptions so the filter still sees them.

Fix
[Authorize]
[AuthorizeForScopes(Scopes = new[] { "User.Read" })]
public class HomeController : Controller
{
    // actions that call ITokenAcquisition, GraphServiceClient or IDownstreamApi
}

Verify

Restart the app while signed in, then open the page. The browser makes a quick round trip to Microsoft Entra and the page loads data instead of the exception.

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 4

User tokens live in an in-memory cache that restarts and scale-out wipe

You seeThe error follows every app recycle, or appears only on some requests behind a load balancer with several instances

Check

AddInMemoryTokenCaches keeps tokens in the process. Microsoft's cache guide says such tokens are not persisted between application restarts and recommends a distributed cache once the app holds user tokens. Each instance in a farm also has its own separate memory, so a request that lands on another instance finds nothing.

Check
grep -rn "AddInMemoryTokenCaches" --include=*.cs .

Fix

Swap to the distributed token cache and back it with Redis or SQL Server. Redis needs the Microsoft.Extensions.Caching.StackExchangeRedis package.

Fix
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
    .AddMicrosoftIdentityWebApp(builder.Configuration)
    .EnableTokenAcquisitionToCallDownstreamApi(new[] { "User.Read" })
    .AddDistributedTokenCaches();

builder.Services.AddStackExchangeRedisCache(options =>
{
    options.Configuration = builder.Configuration["Redis:Connection"];
    options.InstanceName = "tokens-";
});

Verify

Sign in, restart the app or recycle one instance, and call the API again. It succeeds without any redirect, because the token came back from Redis.

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 4

A Blazor Server component calls the API without the consent handler

You seeThe exception appears inside a Blazor component, where [AuthorizeForScopes] has no effect

Check

Components run over a SignalR circuit, so the MVC exception filter never runs. The wiki says Blazor Server apps must register the consent and conditional access handler and pass every token exception to it.

Check
grep -rn "AddServerSideBlazor" --include=*.cs .

Fix

Register the handler at start-up, inject it in each component that calls an API, and hand it the exception in the catch block.

Fix
// Program.cs
builder.Services.AddServerSideBlazor()
    .AddMicrosoftIdentityConsentHandler();

@* in the component *@
@inject MicrosoftIdentityConsentAndConditionalAccessHandler ConsentHandler
@code {
    protected override async Task OnInitializedAsync()
    {
        try { /* call the downstream API */ }
        catch (Exception ex) { ConsentHandler.HandleException(ex); }
    }
}

Verify

After a restart the component redirects once, returns, and renders the API result.

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 4 of 4

The code asks for a scope the tenant administrator never approved

You seeThe exception comes back for one feature even after a fresh sign-in, while other API calls keep working

Check

When an app relies on admin consent and the {resource}/.default scope, any request for a scope beyond what the administrator approved raises MsalUiRequiredException. The wiki points to that as the sign that more permissions need pre-approval.

Fix

Add the missing permission to the app registration and ask the tenant administrator to consent again, or let the user grant it at runtime by listing the extra scope in AuthorizeForScopes on the action that needs it.

Verify

Sign out and in once. The feature that failed now gets its token and the call succeeds.

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Use AddDistributedTokenCaches in every environment that serves real users, and keep AddInMemoryTokenCaches for local runs and app-only tokens.
  • Add a smoke test that signs in, restarts the app and calls one protected page, so a missing AuthorizeForScopes shows before release.
  • Keep the scope list in configuration and add new scopes to the app registration in the same change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.