Skip to content

Blazor consent handler retains stale user after authentication-state changes #4075

Description

Description

In Blazor Server, MicrosoftIdentityConsentAndConditionalAccessHandler.User retains the circuit's initial principal after the application's AuthenticationStateProvider publishes an authentication-state change.

Blazor's cascading authentication state updates correctly, but the Microsoft.Identity.Web consent handler continues returning the original user. This can cause incremental-consent and conditional-access challenges to use stale user information.

Package: Microsoft.Identity.Web 4.15.0
Framework: .NET 10, Blazor Server

Reproduction

A complete, standalone reproduction is available at:

https://github.com/danroth27/IdentityWebBlazorAuthStateRepro

The app uses the published NuGet package and registers the real consent/circuit handler through AddMicrosoftIdentityConsentHandler(). No library internals are copied, mocked, or modified.

With the .NET 10 SDK installed:

git clone https://github.com/danroth27/IdentityWebBlazorAuthStateRepro.git
Set-Location .\IdentityWebBlazorAuthStateRepro
dotnet run --launch-profile http

Open http://localhost:5011, then:

  1. Confirm both table rows initially show Alice, authenticated.
  2. Click Switch to Bob without reloading. Blazor's current user becomes Bob, but the consent handler still shows Alice.
  3. Click Become anonymous. Blazor's current user becomes anonymous and unauthenticated, but the consent handler still shows Alice, authenticated.
  4. Click Start a new circuit. Both rows reset to Alice.

Actual behavior

Action Blazor authentication state Consent handler's user
Initial load Alice, authenticated Alice, authenticated
Switch to Bob Bob, authenticated Alice, authenticated
Become anonymous Anonymous, unauthenticated Alice, authenticated

These results were reproduced in a browser using the published 4.15.0 package.

The buttons use synthetic, circuit-scoped identities and publish completed authentication-state tasks through NotifyAuthenticationStateChanged. They do not perform real sign-in or sign-out, acquire tokens, or require an Entra tenant. This isolates the state-synchronization problem; it does not demonstrate an authorization bypass.

Expected behavior

After an authentication-state change resolves successfully, the consent handler should reflect the current principal, including transitions to an anonymous user.

Relevant implementation

MicrosoftIdentityServiceHandler reads the authentication state when the circuit opens:

var state = await Provider.GetAuthenticationStateAsync().ConfigureAwait(false);
Service.User = state.User;

It does not subscribe to AuthenticationStateProvider.AuthenticationStateChanged, so the stored principal is not refreshed.

Fix considerations

The fix should cover changes after initialization, changes during initialization, out-of-order task completion, failed or canceled state tasks, and subscription cleanup when the circuit closes.

Successful circuit initialization must establish a usable principal. A pending change notification must not cause the initial principal to be discarded while allowing initialization to finish with User unset. That separate race was identified in the unmerged implementation proposed in #4068, not in the published-package reproduction above.

This existing synchronization problem surfaced during the .NET 11 work in #4068 through analyzer BL0013. It should be addressed independently of the SDK, dependency, and target-framework changes.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions